Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

set

Turn shell options on or off (like -x trace, -e stop on error), set the positional parameters, or list all variables.

Level
Everyday Linux
Group
Shell and help
Needs root
No
Package
bash (built-in)

What it does

set -OPTION turns an option on, set +OPTION turns it off. In scripts the most used line is set -euo pipefail (stop on errors, error on unset variables, fail on broken pipes). set -- a b c sets $1 $2 $3. Without arguments, set prints all shell variables and functions.

How to write it

set [-abefhkmnptuvxBCEHPT] [-o OPTION] [--] [ARGUMENT]...

Options 23

Most used

-e, -o errexit
Exit when a command fails.
-u, -o nounset
Error when an unset variable is used.
-x, -o xtrace
Print commands before running them (trace).
-o pipefail
A pipe fails if any command in it fails.
-v, -o verbose
Print lines as they are read.
-n, -o noexec
Read commands but do not run them.
-f, -o noglob
No wildcard expansion.
-C, -o noclobber
> does not overwrite files (use >| to force).
-a, -o allexport
Export all new variables.
-b, -o notify
Report finished jobs at once.
-h, -o hashall
Remember command locations.
-k, -o keyword
All assignments go to the environment.
-m, -o monitor
Job control.
-p, -o privileged
Privileged mode.
-t, -o onecmd
Exit after one command.
-B, -o braceexpand
Brace expansion (on by default).
-E, -o errtrace
ERR traps in functions.
-H, -o histexpand
! history expansion.
-P, -o physical
No symlinks for cd and pwd.
-T, -o functrace
DEBUG/RETURN traps in functions.
-o vi, -o emacs
Editing mode of the command line.
-o
List all options and their state.
--
Set the positional parameters (or clear them).

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Strict mode for scripts

    head -2 /usr/local/bin/backup.sh
    #!/bin/bash
    set -euo pipefail

    Many hidden errors become visible.

  2. 2Trace part of a script

    set -x; cp a b; set +x
    + cp a b

    Only these lines are traced.

  3. 3Stop on errors

    bash -c 'set -e; false; echo "not reached"'

    The script ends at false.

  4. 4Unset variable error

    bash -c 'set -u; echo "$TYPO"'
    bash: line 1: TYPO: unbound variable

    Catches typos like rm -rf "$DIR/" with an empty DIR.

  5. 5Pipe failures

    bash -c 'set -o pipefail; false | true; echo $?'
    1

    Without pipefail the result would be 0.

  6. 6Do not overwrite files

    set -C; echo x > existing.txt
    bash: existing.txt: cannot overwrite existing file

    Protects against a wrong >.

  7. 7vi mode on the command line

    set -o vi

    Esc, then vi keys to edit.

  8. 8Show all options

    set -o | head

    on/off for each.

  9. 9Set positional parameters

    set -- one two three; echo "$2 / $#"
    two / 3

    Useful in tests and functions.

  10. 10List variables and functions

    set | grep ^HIST

    All HIST variables.

Exit codes

0Done.
1Wrong option.