Reference

Shell & command line

Read commands, quoting, streams, paths and scripts.

Scope and conventions

POSIX sh (2017 baseline), Bash additions and named Unix tools. Examples are independent fragments; file paths are illustrative. Bash-only syntax is labeled. macOS and Linux utility flags can differ.

Status & control flow

POSIX sh docs test utility ↗

Read success and failure immediately

POSIX sh

Zero conventionally means success; nonzero means another outcome. Save $? before another command overwrites it.

Fragment

POSIX sh · Read success and failure immediately
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
./check.sh
status=$?
printf 'exit=%s\n' "$status"

Some tools use distinct statuses for no matches and errors.

Run the next command conditionally

POSIX sh

&& runs its right side after success; || runs it after failure. ; sequences commands without requiring success.

Fragment

POSIX sh · Run the next command conditionally
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cd ./project && printf 'ready\n'
./check.sh || printf 'check failed\n' >&2

a && b || c also runs c if b fails; it is not a general if/else replacement.

Test strings, integers and files

POSIX sh

[ is a command: spaces around its arguments matter. = compares strings, -eq compares integers, -f checks a regular file and -d checks a directory.

Fragment

POSIX sh · Test strings, integers and files
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
if [ -f "$file" ]; then
  printf 'found\n'
fi
[ "$count" -eq 3 ] && printf 'three\n'

Quote operands; avoid nonportable == and ambiguous compound tests in [.

Iterate arguments or read complete lines

POSIX sh

for can iterate quoted arguments. IFS= read -r preserves leading spaces and backslashes; the extra condition keeps a final nonempty unterminated line.

Fragment

POSIX sh · Iterate arguments or read complete lines
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for item in "$@"; do
  printf '%s\n' "$item"
done
while IFS= read -r line || [ -n "$line" ]; do
  printf '%s\n' "$line"
done < input.txt

Line-based input is not a safe encoding of arbitrary filenames containing newlines.

Choose a branch with shell patterns

POSIX sh

case matches shell patterns, not regular expressions. ;; ends a branch.

Fragment

POSIX sh · Choose a branch with shell patterns
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
case "$mode" in
  dev|test) printf 'local\n' ;;
  *) printf 'unknown mode\n' >&2; exit 2 ;;
esac

Isolate directory and variable changes

POSIX sh

( commands ) runs in a subshell environment. { commands; } groups commands in the current shell.

Fragment

POSIX sh · Isolate directory and variable changes
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(cd ./project && pwd)
pwd
{ printf 'one\n'; printf 'two\n'; } > lines.txt