LinuxCommandLibrary

set

TLDR

Enable strict mode (exit on error, undefined variables, pipeline failures)

$ set -euo pipefail
copy
Exit immediately if any command returns non-zero status
$ set -e
copy
Treat unset variables as errors
$ set -u
copy
Print commands before execution (debugging)
$ set -x
copy
Disable an option (use + instead of -)
$ set +x
copy
Disable filename expansion (globbing)
$ set -f
copy
List all shell variables and functions
$ set
copy
Set positional parameters
$ set -- [arg1] [arg2] [arg3]
copy

SYNOPSIS

set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [arg...]

DESCRIPTION

set is a shell builtin that controls shell options and positional parameters. Options modify shell behavior for scripting, debugging, and interactive use. Use - to enable an option and + to disable it.
When invoked without arguments, set displays all shell variables and functions. With -- followed by arguments, it sets the positional parameters ($1, $2, etc.) to the provided values.
The combination set -euo pipefail is a common "strict mode" for shell scripts that causes immediate exit on errors, undefined variable references, or pipeline failures—catching bugs early and preventing silent failures.

PARAMETERS

-a (allexport)

Export all created or modified variables and functions
-b (notify)
Report terminated background job status immediately
-e (errexit)
Exit immediately if a command returns non-zero status
-f (noglob)
Disable filename expansion (globbing)
-h (hashall)
Remember command locations as they are looked up
-k (keyword)
Place all assignment arguments in the environment
-m (monitor)
Enable job control
-n (noexec)
Read commands but do not execute them (syntax check)
-p (privileged)
Enable privileged mode
-t (onecmd)
Exit after reading and executing one command
-u (nounset)
Treat unset variables as an error during expansion
-v (verbose)
Print shell input lines as they are read
-x (xtrace)
Print commands and arguments before execution
-B (braceexpand)
Enable brace expansion (default on)
-C (noclobber)
Prevent overwriting files with output redirection
-E (errtrace)
ERR trap is inherited by shell functions and subshells
-H (histexpand)
Enable ! style history substitution
-P (physical)
Do not resolve symbolic links for commands like cd
-T (functrace)
DEBUG and RETURN traps inherited by functions
-o pipefail
Pipeline returns status of last non-zero command
--
End of options; remaining args become positional parameters

CAVEATS

The -e option has many exceptions: it does not trigger exit for commands in if tests, while/until conditions, commands negated with !, or the left side of && and ||. Understanding these exceptions is important for reliable error handling in scripts.

HISTORY

The set command has been part of the Bourne shell since its creation by Stephen Bourne at Bell Labs in 1979. It was included in POSIX and extended in bash with additional options like pipefail. The concept of shell options predates even the Bourne shell, with roots in early Unix shells.

SEE ALSO

bash(1), sh(1), shopt(1), unset(1), export(1)

> TERMINAL_GEAR

Curated for the Linux community

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99
KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

$16.99
nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

$129.95
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

Raspberry Pi 5 8GB

Raspberry Pi 5 8GB

$134.50
New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

$54.00
GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

$739.99