Using Semicolons and Newlines
Bash has to know where one command ends and the next begins. It uses two main separators for this: the newline and the semicolon. They are interchangeable in most places, and understanding exactly how they work explains many of the syntax errors beginners meet.
Commands Are Separated by Newlines or Semicolons
A newline ends a command, so the usual way to write a script is one command per line:
echo one
echo two
A semicolon does the same job within a line, so several commands can share a line:
echo one; echo two; echo three
one
two
three
The two forms are equivalent. Bash runs the commands in order, and each command starts only after the previous one has finished. The exit status of the whole line is the status of the last command, so true; false; echo "status=$?" prints status=1: only the final status survives, and earlier failures are not reported.
A semicolon is not needed at the end of the last command on a line, because the newline already ends it. A trailing comment after a semicolon is fine as well:
echo hi; # this comment is ignored
echo there
hi
there
A Semicolon Always Continues
The semicolon runs the next command no matter what happened before it:
ls /nonexistent_dir 2>/dev/null; echo "after the semicolon"
after the semicolon
The first command failed, but the second still ran. If you want the second command to run only when the first succeeds, use && instead; to run it only when the first fails, use ||. Both of these are explained in a later lesson on command lists, but a short preview shows the contrast:
ls /nonexistent_dir 2>/dev/null && echo "after &&"; echo "end"
end
The echo "after &&" was skipped because ls failed. The semicolon after it then started a fresh command, which ran as usual.
The same pair can create a safe "if this works" chain:
mkdir -p d33 && cd d33 && pwd
Each step runs only if the one before it worked, and the whole chain stops at the first failure.
Semicolons in Compound Commands
Commands such as if, for, and while have several parts, and the parts must be separated. On separate lines, the newline does this automatically:
if [ 1 -eq 1 ]
then
echo "newline style"
fi
newline style
To write the same thing on one line, use a semicolon where the newline was. Here the semicolon after the test [ 3 -gt 2 ] marks the end of the condition so that then can begin:
if [ 3 -gt 2 ]; then echo bigger; fi
bigger
Loops follow the same rule. The keyword do must start a new command, so it needs either a newline or a semicolon before it:
for n in 1 2 3; do echo "n=$n"; done
n=1
n=2
n=3
The multi-line form is more common in scripts because it is easier to read:
i=0
while [ "$i" -lt 2 ]
do
echo "i=$i"
i=$((i + 1))
done
i=0
i=1
Most people put then and do at the end of the first line, after a semicolon, so the keyword and its condition stay together:
for f in a b; do
echo "$f"
done
What Happens When a Separator Is Missing
If the semicolon or newline is left out, Bash reads the keyword as an ordinary word belonging to the previous command, and then it hits something it does not expect. Leaving the semicolon out of an if produces:
if [ 3 -gt 2 ] then echo bigger; fi
syntax error near unexpected token `fi'
Bash read then, echo, and bigger as extra arguments to the [ command, never saw a real then, and complained at fi. The same thing happens with a loop:
for n in 1 2 3 do echo $n; done
syntax error near unexpected token `done'
Here do and the words after it became part of the list of values after in, so the loop never received its do. The exact wording and line numbers of these messages depend on the Bash version and on whether the script is run from a file, but the missing separator is always the cause. When you see "unexpected token", check the line just before the token it names.
Grouping Commands
Curly braces group several commands so that they act as one. The rules are strict: the opening brace must be followed by a space, and the last command inside must end with a semicolon or newline before the closing brace.
{ echo a; echo b; }
a
b
Without the semicolon after echo b, Bash treats } as an ordinary argument to echo and keeps waiting for the real closing brace:
{ echo a; echo b }
syntax error: unexpected end of file
Parentheses create a subshell, which runs the group in a separate child copy of the shell. They do not need the same spacing, and a change such as cd inside them does not affect the surrounding script:
(cd /tmp; pwd)
pwd
/tmp
/home/user/project
The second line shows the original directory, which will be whatever directory you ran the script from. The cd happened only inside the subshell.
The Double Semicolon in case
Inside a case statement, two semicolons (;;) end the commands belonging to one pattern. It is a different symbol with a different meaning, and it is not allowed anywhere else:
x=b
case $x in
a) echo A ;;
b) echo B ;;
*) echo other ;;
esac
B
The same case can be written on a single line, as in case $x in a) echo A ;; b) echo B ;; *) echo other ;; esac. Placing ;; outside a case is a syntax error:
echo a;; echo b
syntax error near unexpected token `;;'
Likewise, a semicolon cannot start a line or follow another separator directly, because Bash does not allow an empty command in front of it:
; echo x
syntax error near unexpected token `;'
Running a Command in the Background
The ampersand (&) also ends a command, but it starts the command in the background instead of waiting for it. The next command begins immediately:
sleep 0.2 & echo started; wait; echo finished
started
finished
echo started ran while sleep was still going. The wait command then paused the script until the background job ended. Background jobs are the subject of a later lesson, so this is only a short look at how & differs from ;.
Splitting a Long Command Across Lines
A long command can be spread over several lines in two ways.
With a backslash at the end of the line. The backslash cancels the newline that follows it, so Bash sees one long line. Nothing, not even a space, may come after the backslash:
echo "one" \
"two" \
"three"
one two three
After an operator that needs more input. If a line ends with |, &&, or ||, Bash knows the command is not finished and continues on the next line without a backslash:
printf 'b\na\n' |
sort
echo a &&
echo b
a
b
a
b
The operator must be at the end of the first line. Starting the second line with it is an error, because the first line was already a complete command:
echo a
| cat
a
syntax error near unexpected token `|'
Note that a was printed before the error, because Bash ran the complete first line as soon as it read it.
Newlines and Semicolons Inside Quotes
Inside quotes, both characters are ordinary text. A quoted string can contain a real newline, and Bash includes it in the string:
echo "line1
line2"
line1
line2
A quoted semicolon is just a character, and it does not separate commands:
echo 'a; b'
a; b
The same is true of an unquoted semicolon that is escaped with a backslash. This is useful when you pass a semicolon as an argument to another program, such as the -exec action of the find command, which requires one.