Session and utilities
The set command
Lists the shell variables, or changes how bash behaves through its options.
Most useful options
-eExits as soon as a command fails. In a script, it stops at the first error-uTreats the use of undefined variables as an error-xPrints each command, already expanded and prefixed with +, before running it-vPrints each script line as it is read, before running it-nReads commands without running them. Useful to check a script's syntax-aAutomatically exports every variable you create or modify-o pipefailMakes a pipeline fail if any of its commands fails, not only the last oneCopy-ready examples
$ setlists the shell variables with their values.$ set -- uno dos tres && echo "$2"sets the positional parameters: $1 is uno and $2 is dos.Example output
dos
$ set -x; echo hola; set +xprints each command prefixed with + before running it, then turns trace mode off.Example output
+ echo hola hola + set +x
$ set -euo pipefaila common combination at the top of a script: stops on errors, undefined variables and failed pipelines.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| bash: set: -Q: invalid option | set has no -Q option. Options are letters, such as -e or -u, and the valid ones appear in the usage line. | Read the usage line printed below the error, or type help set. |
| bash: NOEXISTE: unbound variable | With set -u, using an undefined variable is a fatal error in a script. It also happens with $1 when no arguments were passed. | Define the variable before using it, or use ${NOEXISTE:-value} to give a default value. |
Typical mistakes
- set -e does not stop when the failure is in the condition of an if or a while, or in commands joined with && or ||, except the last one.
- With set -e, only the last command of a pipeline counts, unless you enable pipefail.
- set with no arguments also shows the variables that are not exported. env shows only the exported ones.
Differences from similar commands
- env: shows only the variables that reach programs. set shows every shell variable, including the ones not exported.
- export: marks variables so child programs inherit them. set exports nothing unless you use -a.
declare -p: lists variables with their attributes and values. set shows the values without attributes, and also changes options.
Security
set -x prints each command with its arguments to standard error. If you pass passwords or tokens on the command line, they are visible there; do not leave trace mode on in scripts that handle secrets.
FAQ
Which combination is used at the top of a script?
set -euo pipefail: -e stops on an error, -u on undefined variables, and pipefail makes a pipeline fail if any of its commands fails.
How do I turn a set option off?
Use + instead of -: set +x turns tracing off, and set +e turns off stop-on-error.
How do I see only the exported variables?
With env, or with export -p in bash, which shows the variables marked for export.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course