TExcelTAD
Home › Linux & Bash › Bash Syntax

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:

bash · try it yourself
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:

bash · try it yourself
# This whole line is a comment
echo "Backup started"   # a comment after a command
echo "Issue #42 is not a comment"
echo price#tag
Backup 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
bash · try it yourself
echo first; echo second
mkdir reports && echo "created reports/"
mkdir reports || echo "reports/ already exists"
echo "banana apple cherry" | tr ' ' '\n' | sort
first
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:

bash · try it yourself
printf '%-8s %5s\n' \
  "item" "qty" \
  "apples" 12 \
  "pears" 7
item       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.

bash · try it yourself
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?

  1. a b
  2. a b
  3. ab
  4. An error

In make && echo ok, when is echo ok run?

  1. Always
  2. Only if make succeeds
  3. Only if make fails
  4. Before make

What exit status does Bash set when a command is not found?

  1. 0
  2. 1
  3. 2
  4. 127