Command Chaining in Shell

Level 3114 keystrokes

&& runs the next command only if the previous one succeeded, || only if it failed, and ; always. The output shown assumes temp.txt does not exist; the date line varies.

Shell code

# && chain: each step runs only if the previous one succeeded
# cd build stays in effect for the following lines
1mkdir -p build && cd build && cmake .. && make
# 2>/dev/null hides rm's error; || runs echo only if rm failed
2rm temp.txt 2>/dev/null || echo "File not found"
# ; runs date whatever happened before; the date shown varies
3echo "Done" ; date

Output

File not found
Done
Mon Jan 15 10:30:45 UTC 2024
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • &× 6
  • "× 4
  • .× 3
  • /× 2
  • |× 2
  • -× 1
  • ;× 1
  • >× 1

More Shell examples at level 3

← Shell typing practice