Home

Search IconIcon to open search

Bash quirks

# Bash syntax is a joke

Don’t use spaces before or after = when declaring variables

No:

1
2
msg = "hello"
echo $msg
1
msg: command not found

No:

1
2
msg= "hello"
echo $msg
1
hello: command not found

Yep:

1
2
msg="hello"
echo $msg
1
hello

Use quotes around a variable for multiline output

No:

1
2
3
4
msg="\
hello
multiline"
echo $msg
1
hello multiline

Yep:

1
2
3
4
msg="\
hello
multiline"
echo "$msg"
1
2
hello
multiline

# if

if or if [ ] or if [[ ]] or if [[[ ]]]??? I don’t know!

See Differences Between [ ] and [[ ]] in Bash.