set

shell builtinLinux/Unix
The set command is one of the most frequently used commands in Linux/Unix-like operating systems. set Set or unset shell options and positional parameters

Quick Reference

Command Name:

set

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

set [options] [arguments]

Common Use Cases

    Syntax

    set [options] [arguments]

    Options

    Option Description
    -a Mark variables for export when modified
    -b Report termination of background jobs immediately
    -e Exit immediately if a command exits with non-zero status
    -f Disable filename generation (globbing)
    -h Remember the location of commands as they are looked up
    -k All assignment arguments are placed in the environment for a command
    -m Enable job control
    -n Read commands but do not execute them (syntax check)
    -o option Set a shell option by name
    -p Turn on privileged mode
    -t Exit after reading and executing one command
    -u Treat unset variables as an error when substituting
    -v Print shell input lines as they are read
    -x Print commands and their arguments as they are executed
    - Reset all options and disable special shell handling
    -- Signals the end of options; remaining arguments become positional parameters

    Examples

    How to Use These Examples

    The examples below show common ways to use the set command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    set -e
    Enable the "exit immediately if a command exits with a non-zero status" option.
    set -x
    Enable the "print commands and their arguments as they are executed" option (debugging).
    set +e
    Disable the "exit immediately" option. set -- arg1 arg2 arg3 Set positional parameters ($1, $2, $3) to arg1, arg2, and arg3. # Advanced Examples Advanced set -euo pipefail Commonly used in scripts for strict error handling (exit on error, exit on undefined variable, exit if any command in a pipe fails). set - Reset all shell options to their default values. # Display all shell variables and functions set # Use verbose and xtrace together for detailed debugging set -vx echo "Debugging this script" command1 command2 set +vx # Save and restore options oldstate=$(set +o) set -e # Commands that may fail $oldstate # Restore previous state # Prevent variable exposure in child processes set -a SECRET="sensitive data" set +a readonly SECRET # Parse command output into positional parameters set -- $(ls -la) echo "The first parameter is: $1" echo "The second parameter is: $2" # Working with IFS old_IFS="$IFS" IFS="," set -- "a,b,c" echo "$1 $2 $3" # Outputs: a b c IFS="$old_IFS"

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `set` command is a powerful built-in shell command used to control various aspects of the shell's behavior. It serves multiple purposes, including setting or unsetting shell options, manipulating positional parameters, and displaying all defined variables and functions in the current shell environment. Key functionalities of the `set` command include: 1. Shell Option Management: The `set` command can enable or disable various shell behavioral flags that affect how scripts are executed. Options are typically enabled with a minus sign (`-`) and disabled with a plus sign (`+`). For example, `set -e` enables immediate exit on error, while `set +e` disables it. 2. Positional Parameter Manipulation: Using `set` with the `--` delimiter followed by arguments allows you to set or reset the shell's positional parameters (`$1`, `$2`, etc.). This is particularly useful for parsing command output or splitting strings into separate arguments. 3. Environment Display: When used without options, `set` displays all variables and functions defined in the current shell environment, providing a comprehensive view of the current state. Common use cases for the `set` command include: - Script Debugging: Options like `-x` (xtrace) and `-v` (verbose) are invaluable for debugging scripts by showing commands and their values during execution. - Error Handling: The `-e` option is widely used in scripts to ensure they fail fast when errors occur, rather than continuing with potentially incorrect execution. - Security Hardening: Options like `-u` help prevent scripts from using undefined variables, which could lead to unexpected behavior. - Parameter Processing: Using `set --` to process command output into positional parameters simplifies parsing in shell scripts. The `set -o` form of the command allows for setting options by name rather than by single-letter flags, which can make scripts more readable. For example, `set -o errexit` is equivalent to `set -e`. It's worth noting that the behavior of `set` can vary slightly between different shells (Bash, Zsh, etc.), although the core functionality remains consistent. Additionally, some shell options can also be set using the `shopt` command in Bash, which provides access to additional shell options not available through `set`. When using `set` in scripts, it's a common practice to store the initial state and restore it later if needed, particularly when using options that significantly alter shell behavior. This ensures that scripts don't unexpectedly affect the calling environment.

    Related Commands

    These commands are frequently used alongside set or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the set command works in different scenarios.

    $ set
    View All Commands