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.

Commands & paths

Read a command line

POSIX sh

The first word selects the command; later words become arguments after shell expansion. Options belong to the command, not to the shell.

Fragment

POSIX sh · Read a command line
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
printf '%s\n' "two words"
# printf receives a format and one text argument

Locate the working directory

POSIX sh

Relative paths start at the process working directory. / starts an absolute path; . means here and .. means the parent.

Fragment

POSIX sh · Locate the working directory
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
pwd
ls -la
cd ./project || exit 1
printf '%s\n' ../README.md

Resolve a command through PATH

POSIX sh

command -v reports how the shell resolves a name. PATH lists command search directories, separated by colons.

Fragment

POSIX sh · Resolve a command through PATH
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
command -v git
printf '%s\n' "$PATH"
PATH="$PWD/tools:$PATH"
export PATH

Adding a directory to PATH changes which executable can be selected. Prefer absolute directories: a relative entry (., ./tools or an empty field) resolves against whatever directory the shell is in later.

Local variables and command environments

POSIX sh

Assignment has no spaces around =. Export passes a variable to child processes. A prefix assignment applies to that command invocation, except that for special built-ins such as export, : or . (and in some shells for functions) it can persist in the current shell.

Fragment

POSIX sh · Local variables and command environments
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
mode=preview
export mode
MODE=test ./check.sh

A child process cannot change its parent’s environment.

Find command documentation

POSIX sh

Use the installed utility’s manual for its actual flags. Bash help documents Bash builtins; it is not a portable sh command.

Fragment

POSIX sh · Find command documentation
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
man printf
man find