What does set do in Bash?

The Bash shell includes many built-in commands geared towards manipulating the environment of a running shell session.

The set command allows the following:

  • To view the names and values of shell variables and functions.

  • To change the shell environment variables and options.

The command syntax

The syntax of the set command is as follows:

set [options] [arguments]

Options

The options are a set of mutually exclusive flags to change the defined shell scripts' functioning mode.

  • set -option: Add the selected option.

  • set +option: Unset the selected option.

The following table lists the most common set command options:

Option

Description

-a

Mark subsequently created or modified variables or functions for export.

-b

Alert a user when a background job has been terminated.

-e

Exit the shell if a command fails (i.e. when it returns a non-zero status).

-f

Disable the globbing or the pathname expansion.

-h

Identify and save function commands upon defining a function.

This option is enabled by default.

-k

Include all assignment arguments, not just those preceding the command name, in the command environment.

-n

Commands are read but have yet to be executed.

-m

When a task gets completed, a message is displayed.

-p

Turn off the $ENV file processing and the importing of shell functions.

In the event that the effective and real user IDs do not match, this option is enabled by default.

-t

Read one command and exit subsequently.

-u

Exit when undefined or unset variables are invoked.

-v

Print out shell input lines.

-x

Display command arguments when executed.

These options can be combined together if required. For example, the command set -ef disable path expansion and simultaneously exit on command failure.

Arguments

  • The arguments i.e. $1 $2..$n are positional parameters that are assigned to command-line arguments passed to a script or a function.

  • The first positional parameter refers to $1, the second refers to $2 and henceforth.

Exit values

The set command yields the following exit values:

Value

Description

0

Indicate a successful execution.

1

Indicate a failure stemming from an invalid argument.

2

Indicate a failure generating a usage message usually occurs when an argument is missing.

The exit value can be accessed using the special shell variable $?.

Examples

For demonstration purposes, let's go through the following examples to get insight into the basic usage of this command:

Without options

By default, running the set command without any options or arguments returns a list of all settings, including the Bash executable location, version info, and environment variables like PATH.

set

Using the -a option

With the -a option, all variables defined after the set command are automatically exported, as shown below:

set -a
x=5
y=10
z=15
echo $x, $y, $z

Using the -o option

For debugging purposes, we may turn on the command tracing using the following options:

set -x or set -o xtrace

When enabling this option, all commands that are subsequent to the set command will be displayed directly before execution as per the while loop shown below:

set -x
v=3
while [ $v -gt 0 ]; do
v=$[ $v-1 ]
echo $v
done

Using the -e option

Let's now try the -e option to exit the shell script when an error occurs during execution.

set -e
#Read data from an unexisting file
cat unexistingfile
#The script exited after the cat command failure
echo "Step unreached"

Since the file unexistingfile does not exist, an error is raised, and the last echo command will not run.

Using the -u option

Let's check out the -u option to report the errors occurring.
By default, if a variable is not defined, Bash ignores such a flaw, but an error is produced with the -u option.

set -u
v1="123"
echo $v1 $v2

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved