intro-to-bash

October 15, 2019 ยท View on GitHub

Recommended Resources

Hot Tips

How to use variables correctly

Spacing is critical in bash since commands are separated by spaces. Assignments, for example, should be done without spaces.

# Bad. Interprets line as 3 separate commands.
var = 'lol'

# Good. Will assign 'lol' to var.
var='lol'

Always use lower_case for local variables and UPPER_CASE for environment variables.

# local variable.
my_local_variable='lol'

# environment variable.
export MY_ENV_VARIABLE='lol'

Always use single quotes for string literals and double quotes for interpolated strings.

cat='sunny'
lol='hello $cat'
echo "$lol"

# prints
# hello $cat
cat='sunny'
lol="hello $cat"
echo "$lol"

# prints
# hello sunny

Always double quote variables unless you know what you're doing. The only exceptions are for loops and the test expression syntax with double brackets, e.g. [[ $name = 'cat' ]].

words="good boy does fine"

# Bad. Executes 4 separate commands with 1 word arg.
ls $words

# Good. Executes a single command with 4 args.
ls "$words"