The Linux set command is a built-in shell command that allows you to display or set both shell and environment variables. In this guide, we cover the set command and demonstrate the various ways that the command-line utility can be used.

Basic Syntax

The set command takes the following syntax:

$ command -options arguments

Command Options

There are quite a number of options that can be used with the set command. Let’s explore some of them:

  • -a:   The -a option sets all created or modified variables or functions for export.
  • -b:   The -b option immediately alerts the user when the background jobs are terminated.
  • -e: The -e option instructs a shell to exit if a command yields a non-zero exit status. Simply put, the shell exits when the command fails.
  • -f: The -f option disables the generation of filenames.
  • -h: The -h option is enabled by default. It locates and then remembers a function as it is awaiting execution.
  • -n: The -n option only reads commands but fails to execute them.
  • -t: The option -t exits upon reading and running one command.
  • -u: The -u option treats unset or undefined variables except for special parameters such as wildcards (*) or “@” as errors during parameter expansion.
  • -v: The -v option prints out the lines of the shell input as they are being read.
  • -x: The -x option prints command arguments during execution

Exit Values

The following are the shell exit values associated with the set command:

0: Command was successful.

  1.  Command failed due to an incorrect command argument
  2. Command failure due to an expected argument that is missing

Set Command Without Any Options

Without any arguments, the set command lists all the shell variables, including their values.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-41.png" data-lazy- height="462" src="data:image/svg xml,” width=”753″>

Set Positional Parameters With the Set Command

The Linux set command can be used to assign values to positional parameters. A positional parameter is a variable in a shell program, and its value is referenced as ${N} where N is a digit denoting the position of the parameter.

The $1 value is the first positional parameter after the name of the file or command. The $2 value is the second parameter, and so on.

Suppose we execute the command shown below:

Here, red corresponds to positional parameter $1, blue corresponds to parameter $2, and finally, green corresponds to  $3.

To list all the parameters in the order of $1 $2 $3 run the echo command below:

To list the first parameter, execute:

To list the second parameter, run:

And so on.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-41.png" data-lazy- height="393" src="data:image/svg xml,” width=”752″>

Use Set Command to Unset All Positional Parameters

To unset the positional parameters run the set command with double hyphens — as shown.

Once again, if you try to list the positional parameters, you will get blank output, implying that they have been unset.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-41.png" data-lazy- height="383" src="data:image/svg xml,” width=”746″>

Ignore An Unbound Variable

By default, a shell script overlooks an undefined variable. In the script myscript.sh shown below, the $foo variable is not yet defined and therefore, doesn’t exist.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-35.png" data-lazy- height="219" src="data:image/svg xml,” width=”728″>

When the script is run, it returns a blank line for the line that contains a non-existent variable and proceeds to execute the following line:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-40.png" data-lazy- height="188" src="data:image/svg xml,” width=”725″>

This anomaly is undesired, and developers would want to be notified in case of undefined variables. The set -u directive at the start of the script will print out an error on the shell if the script runs into an undefined variable.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-25.png" data-lazy- height="212" src="data:image/svg xml,” width=”728″>

When the script is run yet again, the error about an unbound variable is displayed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-32.png" data-lazy- height="178" src="data:image/svg xml,” width=”725″>

Display an Error If a Command Is Non-existent

Usually, if a command runs into an error and fails to execute, the bash shell will continue to execute the remaining commands. Take, for instance, the shell script below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-18.png" data-lazy- height="209" src="data:image/svg xml,” width=”656″>

The command foobar is non-existent, and an error should be displayed on the bash shell when the script is executed to show that the script into a problem. However, this does not happen and the shell goes along to execute the next line as shown:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-20.png" data-lazy- height="177" src="data:image/svg xml,” width=”656″>

Like the previous example, this is not good practice when writing shell scripts, especially for security and debugging. Ideally, the script should halt when it encounters an error.  To address this scenario, define the directive set -e at the start of the script as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image12-9.png" data-lazy- height="239" src="data:image/svg xml,” width=”729″>

When you try to run the script again, you will run into the error as shown:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-12.png" data-lazy- height="188" src="data:image/svg xml,” width=”727″>

Display an Error in Piped Commands

The directive set -e does not work when dealing with piped commands. Consider the script below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image11-9.png" data-lazy- height="248" src="data:image/svg xml,” width=”729″>

When you run the script, it returns an error but continues to run the subsequent command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image13-9.png" data-lazy- height="243" src="data:image/svg xml,” width=”727″>

To overcome this hurdle, pass the set -eo pipefail directive as shown:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image14-6.png" data-lazy- height="255" src="data:image/svg xml,” width=”724″>

This time around, the script terminates and doesn’t execute the next line.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image15-4.png" data-lazy- height="242" src="data:image/svg xml,” width=”729″>

Define Allexport and Notify Options

To set allexport and notify options, run the command:

$ set -o allexport -o notify

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image16-3.png" data-lazy- height="140" src="data:image/svg xml,” width=”751″>

Conclusion

Those were a few examples of how you can use the set command in your shell scripts. As observed, the set command can be a handy tool in setting positional parameters and debugging your shell scripts.