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.

Scripts & processes

POSIX sh docs getopts utility ↗

Define a function and return a status

POSIX sh

Functions run in the current shell environment unless invoked in a subshell. return exits a function; exit ends the shell process.

Fragment

POSIX sh · Define a function and return a status
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
greet() {
  printf 'Hello, %s\n' "$1"
}
greet "Ada Lee"

Execute versus source a script

POSIX sh

sh file runs a script in a new shell. . file reads it in the current environment. A shebang selects an interpreter when a script is executed directly.

Fragment

POSIX sh · Execute versus source a script
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#!/bin/sh
# Save this in a script file.
printf '%s\n' "$0"

Use #!/usr/bin/env bash for Bash scripts when Bash is resolved through PATH. Do not source an untrusted file.

Start background work and wait for it

POSIX sh

& starts asynchronous work. $! is the most recent background process ID. wait returns its completion status.

Fragment

POSIX sh · Start background work and wait for it
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
./check.sh &
check_pid=$!
wait "$check_pid"
status=$?

Track the process you started; a port number alone is not process ownership.

Handle termination and cleanup

POSIX sh

trap installs a shell action for a signal or shell exit. SIGTERM requests termination; a program may handle it. SIGKILL cannot be trapped.

Fragment

POSIX sh · Handle termination and cleanup
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
trap 'printf "%s\n" "leaving" >&2' 0
trap 'exit 130' INT
trap 'exit 143' TERM

Cleanup should be safe to repeat; signals do not guarantee every descendant has stopped.

Understand script failure options

POSIX sh

set -u reports many unset-parameter uses. set -e exits on some failures but has context-dependent exceptions, including condition tests and parts of command lists.

Fragment

POSIX sh · Understand script failure options
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
set -u
: "${CONFIG:?Set CONFIG}"
if ./check.sh; then
  printf 'passed\n'
else
  printf 'failed\n' >&2
fi

Check important statuses explicitly; set -e does not make every failure fatal.

Read and consume script arguments

POSIX sh

$0 names the script or shell invocation; $1 is the first argument and $# is the remaining count. Braces are required for ${10}. shift removes leading positional arguments.

Fragment

POSIX sh · Read and consume script arguments
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
printf 'script=%s count=%s\n' "$0" "$#"
if [ "$#" -gt 0 ]; then
  first=$1
  shift
fi
printf 'remaining=<%s>\n' "$@"

Check the count before reading a required parameter or shifting.

Parse portable short options

POSIX sh

getopts parses declared short options; a colon after a letter requires its argument. A leading colon enables explicit missing/unknown-option handling. OPTIND is the next argument index.

Fragment

POSIX sh · Parse portable short options
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
verbose=0
output=out.txt
while getopts ":vo:" option; do
  case "$option" in
    v) verbose=1 ;;
    o) output=$OPTARG ;;
    :) printf 'missing value: -%s\n' "$OPTARG" >&2; exit 2 ;;
    \?) printf 'unknown option: -%s\n' "$OPTARG" >&2; exit 2 ;;
  esac
done
shift "$((OPTIND - 1))"

This parses short options, not GNU-style long options. Reusing getopts in one shell may require resetting OPTIND=1.

Know which function variables remain visible

POSIX sh

POSIX sh has no portable local declaration. Function assignments affect the current shell environment; use explicit names or a subshell when isolation is required.

Fragment

POSIX sh · Know which function variables remain visible
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
inspect() (
  directory=$1
  cd "$directory" || exit 1
  pwd
)

This function body uses a subshell, so assignments and directory changes do not escape.

Inspect a process and request termination

POSIX sh

ps can show a known process’s identity and parent. kill sends a signal; TERM requests termination and allows cleanup. Exit is not guaranteed until you observe completion.

Fragment

POSIX sh · Inspect a process and request termination
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ps -p "$worker_pid" -o pid,ppid,etime,comm
kill -TERM "$worker_pid"
wait "$worker_pid"

worker_pid must identify your own recorded child process for wait. PID reuse matters; inspect identity when operating later.