Shell
A program that interprets user commands.
POSIX shell:
#todo https://en.wikipedia.org/wiki/Shebang_(Unix)
✏️ Note
In most shells&&
and||
have the same precedence.
💡 Hint
Add&
at the end of a command to run it in the background.
Best practices
- Write POSIX shell
- Use
set -euf
: exit on first error, disallow unknown variables, disable globing - Use
cd "$(dirname "$0")"
to run the script from its directory - Use
=
for string comparisons,-eq
for numeric comparisons - Always lint scripts with ShellCheck
More: https://github.com/SixArm/unix-shell-script-tactics
Glob patterns
?
: any single character*
: any number of characters**
: any number of characters including/
Sourcing vs Executing
Sourcing a script (. script.sh
) will run its commands in the current shell process.
Changes to the environment are visible in the current shell.
Executing a script (./script.sh
) will run its commands in a new shell process.
Changes to the environment are not visible in the current shell and lost when the script is finished.