Bash Syntax
Bash reads your input as lines of words. The first word is the command, the rest are its arguments, and spaces separate them. Most surprises in Bash come from a few simple rules about how a line is split and when it ends, so this lesson covers those rules first.
Words and spaces
Bash splits a command line on spaces and tabs. Extra spaces between words don't matter, but spaces inside an argument do, so you quote arguments that contain them:
echo one two
echo "one two"
echo 'single quotes keep $HOME literally'
echo "double quotes expand $HOME"one two
one two
single quotes keep $HOME literally
double quotes expand /home/learner
The first echo receives two arguments and prints them separated by a single space. The quoted one receives one argument that keeps its spaces. The quoting lesson goes deeper; for now remember: when in doubt, use double quotes.
Comments
Everything after a # at the start of a word is a comment and is ignored:
# This whole line is a comment
echo "Backup started" # a comment after a command
echo "Issue #42 is not a comment"
echo price#tagBackup started
Issue #42 is not a comment
price#tag
A # inside quotes, or in the middle of a word (price#tag), is just a character.
Several commands on one line
A newline ends a command. You can also separate commands with ;, or connect them with operators that depend on success:
| Separator | Meaning |
|---|---|
a ; b |
Run a, then b |
a && b |
Run b only if a succeeded |
a || b |
Run b only if a failed |
a | b |
Send the output of a into b (a pipe) |
a & |
Run a in the background |
echo first; echo second
mkdir reports && echo "created reports/"
mkdir reports || echo "reports/ already exists"
echo "banana apple cherry" | tr ' ' '\n' | sortfirst
second
created reports/
mkdir: cannot create directory ‘reports’: File exists
reports/ already exists
apple
banana
cherry
The second mkdir fails, prints an error message and the || branch runs. Notice the error starts with the command name: mkdir: cannot create directory .... Read error messages carefully: they nearly always say exactly what went wrong.
Long lines
A backslash \ as the very last character of a line joins it with the next one. It is the standard way to split long commands for readability:
printf '%-8s %5s\n' \
"item" "qty" \
"apples" 12 \
"pears" 7item qty
apples 12
pears 7
Note: The backslash must be the last character. A space after it breaks the continuation, and that kind of invisible mistake is hard to spot.
Case sensitivity
Linux is case-sensitive everywhere: commands, options, file names and variables. echo works, ECHO doesn't, and Report.txt and report.txt are two different files.
ECHO hello
echo "exit status was $?"
touch Report.txt report.txt
ls/w/main.sh: line 1: ECHO: command not found
exit status was 127
Report.txt
report.txt
When Bash can't find a command it prints command not found and sets the exit status $? to 127. (main.sh in the message is the name under which this course saves each example script.) Exit statuses are covered in detail later: 0 means success, anything else means failure.
Common mistakes
| Mistake | Problem | Fix |
|---|---|---|
name = "Ada" |
Bash runs a command called name |
No spaces around =: name="Ada" |
cd My Documents |
Two arguments | cd "My Documents" |
rm -rf $dir/ with empty $dir |
Becomes rm -rf / |
Quote and check variables ("${dir:?}") |
Smart quotes “...” pasted from a web page |
Not shell quotes at all | Retype them as " |
Test yourself
What does echo a b print?
a ba bab- An error
In make && echo ok, when is echo ok run?
- Always
- Only if
makesucceeds - Only if
makefails - Before
make
What exit status does Bash set when a command is not found?
- 0
- 1
- 2
- 127