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.

Arguments & expansion

Single quotes and double quotes

POSIX sh

Single quotes preserve literal characters. Double quotes allow parameter, arithmetic and command substitutions while keeping their result in one argument.

Fragment

POSIX sh · Single quotes and double quotes
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
name="Ada Lee"
printf '%s\n' '$name' "$name"

The first value is the literal $name; the second is Ada Lee.

Keep one value in one argument

POSIX sh

Quote expansions when a value should stay one argument. Unquoted parameter expansion can undergo field splitting and pathname expansion.

Fragment

POSIX sh · Keep one value in one argument
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
file="notes draft.txt"
cat "$file"
printf '<%s>\n' ""

An empty quoted expansion preserves an empty argument.

Forward arguments without changing boundaries

POSIX sh

"$@" expands each positional parameter as a separate argument. "$*" joins them into one word using the first IFS character.

Fragment

POSIX sh · Forward arguments without changing boundaries
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
forward() {
  printf '<%s>\n' "$@"
}
forward "one two" "" three

Arguments are one two, an empty string, and three.

Defaults, required values and string edits

POSIX sh

${x:-fallback} substitutes a default when x is unset or empty. := also assigns; :? reports an error. % removes the shortest matching suffix.

Fragment

POSIX sh · Defaults, required values and string edits
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
label=${LABEL:-local}
: "${CONFIG:?Set CONFIG first}"
file=report.txt
printf '%s\n' "${file%.txt}"

Capture command output

POSIX sh

$(...) captures stdout and removes trailing newlines. Quote the substitution to preserve spaces and internal newlines as one argument.

Fragment

POSIX sh · Capture command output
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
current=$(pwd)
printf '%s\n' "$current"

Evaluate integer arithmetic

POSIX sh

$((...)) performs shell integer arithmetic. It is not floating-point calculation.

Fragment

POSIX sh · Evaluate integer arithmetic
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
count=3
next=$((count + 1))
printf '%s\n' "$next"

Glob patterns versus literal text

POSIX sh

An unquoted * or ? can expand to matching pathnames. A quoted pattern is passed literally to the command, which may interpret it itself.

Fragment

POSIX sh · Glob patterns versus literal text
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
printf '%s\n' ./*.txt
find . -name '*.txt'

In POSIX sh, an unmatched glob remains literal. Bash options can change that behavior.

Separate options from file operands

POSIX sh

Many tools accept -- to end options. A ./ prefix also prevents a filename beginning with - from looking like an option.

Fragment

POSIX sh · Separate options from file operands
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat ./-notes.txt
rg -F -- '--timeout' ./src

-- is utility-specific; check the installed command.