Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash command lists: ;, &&, || and &

Reference for Bash command lists: run commands in sequence with ;, chain on success with &&, on failure with ||, in the background with &, and group with braces.

A list is one or more pipelines joined by the control operators ;, &, && and ||. Lists decide in which order commands run and whether one command depends on the success of another. They are the shell's lightweight alternative to an if statement for simple cases.

Every command ends with an exit status: 0 means success, anything else means failure. && and || read that status; ; and a newline ignore it.

Terminal
cmd1 ; cmd2      # always run both, in order
cmd1 && cmd2     # cmd2 only if cmd1 succeeded
cmd1 || cmd2     # cmd2 only if cmd1 failed
cmd1 &           # run cmd1 in the background

Try it

Bash Sequence, AND and OR

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Background jobs and grouping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
pipeline ; pipeline        # or a newline
pipeline & pipeline
pipeline && pipeline
pipeline || pipeline
{ list; }                  # group, current shell
( list )                   # group, subshell
Operator Description
; or newline Run the left side to completion, then the right.
& Start the left side as a background job and continue immediately; $! holds its PID.
&& Run the right side only if the left exit status is 0.
|| Run the right side only if the left exit status is not 0.
{ list; } Run the commands as a group in the current shell; a ; or newline before } is required and spaces around braces are mandatory.
( list ) Run the commands in a subshell: variable and directory changes do not leak out.

Returns: the status of the last command executed in the list.

Usage notes

  • && and || have equal precedence and associate from the left. a && b || c means (a && b) || c, so c also runs if a succeeds but b fails. Do not treat it as a true if/then/else; use if when b can fail.
  • ; binds looser than && and ||, and & also separates lists. So a && b & c runs a && b in the background and then c.
  • Under set -e, a failing command that is part of a && or || list (except the last one) does not stop the script. That is intended, but it makes failures easy to miss.
  • cd dir && command is safer than cd dir; command: if cd fails, the second form runs the command in the wrong directory.
  • Use wait to collect background jobs. A script that ends without waiting leaves them running (or kills them if the terminal closes).
  • Braces and parentheses differ in cost and effect: ( ) forks a subshell, { } does not. A subshell is the right tool to change directory temporarily.
  • A trailing & or ; is allowed; a leading one is a syntax error. Two ;; in a row are only valid inside case.
  • ! before a pipeline negates its status and is helpful with &&: ! grep -q x file && echo "absent".