Home > Environment & Utility > set

set: Set and Unset Shell Options

The `set` command is used to set or unset various options that control the behavior of the current shell. It is an essential command for improving script stability and debugging efficiency. It can also be used to set positional parameters.

Overview

The `set` command allows for fine-tuning the execution environment of shell scripts. It provides various functionalities such as stopping scripts on errors, preventing the use of undefined variables, and tracing command execution.

Key Features

  • Setting and unsetting shell options
  • Setting positional parameters
  • Supporting script debugging

Key Options

The `set` command offers various single-character options and long-named options via `-o`. Here are some commonly used options.

Script Control Options

-o Options (Long Names)

Generated command:

Try combining the commands.

Description:

`set` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Exit Script Immediately on Error

#!/bin/bash
set -e

echo "Starting"
false # This command returns a non-zero exit code.
echo "This message will not be printed."

Configures the script to terminate immediately if an error occurs.

Error on Using Undefined Variables

#!/bin/bash
set -u

echo "Starting"
echo "$UNDEFINED_VAR" # This variable is undefined, causing an error.

Ensures the script terminates if an uninitialized variable is used.

Trace Command Execution (Debugging)

#!/bin/bash
set -x

VAR="Hello"
echo "$VAR World"
ls -l /tmp

Prints all commands executed by the script, aiding in debugging.

Detect Pipeline Errors

#!/bin/bash
set -o pipefail

echo "Starting"
false | echo "This message will be printed, but the script will be considered failed."
# If set -e is also used, the script will exit at false.

Ensures that if an intermediate command in a pipeline fails, the entire pipeline is considered to have failed.

Common Powerful Combination

set -euo pipefail

A recommended combination for writing robust shell scripts.

Check Current Shell Options

set -o

Displays the status of all options currently set in the shell.

Set Positional Parameters

set -- "apple" "banana" "cherry"
echo "First parameter: $1"
echo "All parameters: $@"

You can manually set the script's positional parameters ($1, $2, etc.) using `set --`. This is useful for testing or in specific situations.

Tips & Precautions

The `set` command can significantly enhance the robustness of shell scripts, but it requires careful usage.

Recommended Combination

For most shell scripts, it is highly recommended to add the `set -euo pipefail` combination at the beginning of the script to improve stability.

  • set -e: Exits immediately on error, preventing unexpected behavior.
  • set -u: Detects potential bugs from using undefined variables early.
  • set -o pipefail: Detects intermediate command failures in a pipeline, accurately determining the success of the entire pipeline.

Unsetting Options

You can unset specific options using `+`, such as `set +e` or `set +u`. This is useful when you need to temporarily change an option's behavior for a specific part of the script. For example, if a particular command must execute even if it fails, you can use `set +e`, and then restore it with `set -e` after the task is complete.

Usage in Debugging

`set -x` is very useful for debugging as it shows the script's execution process in detail. It's advisable to add it at the beginning of the script and remove or comment it out after debugging. `set -v` prints input lines as they are read, helping to understand the script's flow.

Positional Parameters and `--`

`set --` sets all subsequent arguments as positional parameters ($1, $2, etc.) and ensures that these arguments are not interpreted as options, even if they start with a `-`. This is particularly important when redefining `$@` or `$*` within a script.


Same category commands