Linux Commands

The Linux Set Command

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.

$ set

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:

$ set red blue green

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:

$ echo$*

To list the first parameter, execute:

$ echo $1

To list the second parameter, run:

$ echo $2

And so on.

Use Set Command to Unset All Positional Parameters

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

$ set --

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

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.

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:

$ ./myscript.sh

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.

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

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:

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:

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.

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

Display an Error in Piped Commands

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

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

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

$ set -eo pipefail

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

Define Allexport and Notify Options

To set allexport and notify options, run the command:

$ set -o allexport -o notify

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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.