Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using the Linux set command

How-To
Sep 02, 20214 mins
Linux

A lost businessman wanders amid conflicting directional signs through the fog.
Credit: Gremlin / Getty Images

The Linux set command allows you to change the value of shell options or to display the names and values of shell variables. Rarely used, it is a bash builtin, but is quite a bit more complicated than most builtins.

If you use the command without any arguments, you will get a list of all the settings—the names and values of all shell variables and functions. Watch out though! You’ll end up with a torrent of output flowing down your screen. There are just short of 3,000 lines of output on my Fedora system:

$ set | wc -l
2954

The top of the list looks like what you see below, but the output gets considerably more complicated as you move through it.

$ set | head -10
BASH=/bin/bash
BASHOPTS=cdspell:checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=([0]="0")
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="11")
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="5" [1]="1" [2]="0" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")

More practically, there are some very helpful things you can do with set. Let’s examine some of them.

Debugging your scripts

You can use the set -x command to do some script debugging. In other words, when you use this option, bash is going to show you a lot more output that displays what your script is up to. The script used in this example does a lot of checking and then prints the top lines of the selected file.

#!/bin/bash
set -x

if [ $# -lt 2 ]; then
    echo “Usage: $0 lines filename”
    exit 1
fi
if [ ! -f $2 ]; then
    echo “Error: File $2 not found”
    exit 3
else
    echo top of file
    head -$1 $2
fi

msg="bye"
echo $msg

Without the set -x, the script would display output like this:

$ script3 3 file
top of file
#!/bin/bash -x

date
bye

With the set -x, it shows each command as it’s being run as well as the output.

$ script3 3 file
+ ‘[‘ 2 -lt 2 ‘]’
+ [[ 3 != [0-9]* ]]
+ ‘[‘ ‘!’ -f file ‘]’
+ echo top of file
top of file
+ head -3 file
#!/bin/bash -x

date
+ msg=bye
+ echo bye
Bye

You can also invoke debugging by placing the -x on the “shebang line” (i.e., the top line of the script) like this:

#!/bin/bash -x

One of the benefits of using set -x and then set +x is that the first set starts the debugging and the second set turns it off, so you can see the verbose output for a small section of a script if that is all you need to focus on.

set -x
if [ ! -f $2 ]; then
    echo “Error: File $2 not found”
    exit 3
else
    echo top of file
    head -$1 $2
fi
set +x

Other set options can be turned on and then off again in this same manner.

Automatic exporting

Using set -a, you can cause any variable or function that you create to be automatically exported so that subshells and scripts can use them. Here’s an example:

$ set -a
$ one=1
$ two=2
$ three=3
$ four=4
$ /bin/bash	

Exit immediately if a command fails

The set -e command will cause a script to exit as soon as it runs into an error. In this example, the set -e command is invoked.

$ cat script1
#!/bin/bash

set -e
cat nosuchfile
echo “So long!”

As you might suspect, the “nosuchfile” file doesn’t exist, so the script exits at that point with the help of set -e. The final echo command never gets a chance to run.

$ script1
cat: nosuchfile: No such file or directory

Don’t ignore non-existent variables

By default, bash will ignore any variables that don’t exist. For example, in the script below, bash will overlook the fact that $var2 has not been defined and will simply display $var1.

$ cat script1
#!/bin/bash
var1="123"
echo $var1 $var2
$ ./script1
123

If you add set -u to the script, it reports the problem.

$ cat script2
#!/bin/bash

set -u
var1="123"
echo $var1 $var2
$ ./script2
./script1: line 5: var2: unbound variable

Many additional set options are available, but these are some of the most useful.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.