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 syntax of the set command is as follows:
set [options] [arguments]
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 |
| Mark subsequently created or modified variables or functions for export. |
| Alert a user when a background job has been terminated. |
| Exit the shell if a command fails (i.e. when it returns a non-zero status). |
| Disable the globbing or the pathname expansion. |
| Identify and save function commands upon defining a function. This option is enabled by default. |
| Include all assignment arguments, not just those preceding the command name, in the command environment. |
| Commands are read but have yet to be executed. |
| When a task gets completed, a message is displayed. |
| Turn off the In the event that the effective and real user IDs do not match, this option is enabled by default. |
| Read one command and exit subsequently. |
| Exit when undefined or unset variables are invoked. |
| Print out shell input lines. |
| 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.
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.
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 $?
.
For demonstration purposes, let's go through the following examples to get insight into the basic usage of this command:
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
-a
optionWith the -a
option, all variables defined after the set command are automatically exported, as shown below:
set -ax=5y=10z=15echo $x, $y, $z
-o
optionFor 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 -xv=3while [ $v -gt 0 ]; dov=$[ $v-1 ]echo $vdone
-e
optionLet's now try the -e
option to exit the shell script when an error occurs during execution.
set -e#Read data from an unexisting filecat unexistingfile#The script exited after the cat command failureecho "Step unreached"
Since the file unexistingfile
does not exist, an error is raised, and the last echo command will not run.
-u
optionLet'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 -uv1="123"echo $v1 $v2
Free Resources