Small examples of bash scripting.
Element | Notation | Example | Notes |
---|---|---|---|
Constants | SCREAMING_SNAKE_CASE | DEST_PATH | Use readonly or declare -r to ensure they are readonly. |
Environment variable names | SCREAMING_SNAKE_CASE | PATH | |
File | snake_case | my_script | Executables should not have extension (strongly preferred) or a .sh extension.1 |
Functions | snake_case | do_something(){ } | The keyword function it's optional, but must be used consistently troughout a project.2 |
Hashbang | #!/usr/bin/env bash | #!/usr/bin/bash asumes it's always installed in /bin, which can cause issues.3 | |
Local variables | snake_case | my_local_variable | Ensure that local variables are only seen inside a function and it's children by using local when declaring them. |
Variables | snake_case | user_name |
: '
line1
line2
'
bash -x ./script
#/usr/bin/env bash -x
set -x
echo "Code block"
set +x
Always check return values and give informative return values.
For unpiped commands use $?
or check directly via an if
statement.
if ! mv "${file_list[@]}" "${dest_dir}/"; then
echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
exit 1
fi
# Or
mv "${file_list[@]}" "${dest_dir}/"
if (( $? != 0 )); then
echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
exit 1
fi
Given the choice between invoking a shell builtin and invoking a separete process, choose the builtin.
# Prefer this:
addition=$(( X + Y ))
substitution="${string/#foo/bar}"
# Instead of this:
addition="$(expr "${X}" + "${Y}")"
substitution="$(echo "${string}" | sed -e 's/^foo/bar/')"
Use ShellCheck to get warnings and suggestions for your scripts.
If you find these examples useful, you can star this repo.