variables
export
Marks a shell variable (or function with -f) for export to the environment of subsequently executed commands. Without arguments, prints all exported variables in a re-importable format.
export [-fnp] [NAME[=VALUE] ...] Common flags / operators
| Flag / Operator | Purpose |
|---|---|
| -p | Print exported names in reusable form |
| -n | Remove export attribute (variable stays set) |
| -f | Export a function instead of a variable |
| NAME=VALUE | Assign and export in one step |
Examples
export PATH="$HOME/bin:$PATH" Prepend ~/bin to PATH for child processes.
export EDITOR=vim Set EDITOR so tools like git and crontab pick it up.
export -f myfunc Make myfunc available in subshells.
export -n TMP_VAR Unexport TMP_VAR — it stays in the current shell only.
Gotcha
'FOO=bar cmd' sets FOO only for that one command — no export needed. Exported variables can leak secrets into subprocesses; use temporary env prefixes instead when possible.
Related
unset
Removes a variable or function from the shell — different from setting it to empty. Without a flag it targets variables first, then functions, unless -f or -v is specified.
declare / typeset / readonly
Declares variables with attributes: arrays, associative arrays, integers, readonly, lowercase/uppercase, exported. 'readonly' is a shortcut for 'declare -r' that locks a variable so it cannot be reassigned or unset for the lifetime of the shell.
set (options)
Changes shell option flags and/or replaces positional parameters. The 'safer bash' idiom 'set -euo pipefail' is the most common use — it turns on error-exit, unset-variable-error, and pipeline failure propagation.
local
Declares a variable scoped to the current function, hiding any global of the same name during the function's execution. Accepts the same attribute flags as declare (-a, -A, -i, -r, -n, ...).
shift
Renames positional parameters: $2 becomes $1, $3 becomes $2, and so on, decrementing $#. Optional N shifts by N positions and is common in manual argument parsing loops.