How to Use set Command in the Bash Shell
The Bash shell contains several useful built-in commands to manipulate the environment of a currently running shell session. The built-in set
command provides the ability to view and change shell environment variables and options.
This tutorial discusses the range of uses for the set
command and how to effectively write Bash scripts to set up good environment options.
Command Tracing With set -x
in Bash
By default, running the set
command alone will return a list of the currently set variables and their values, including the Bash executable location, version info, and environment variables like PATH.
The use of the command is when you view the executed commands. If you’re a programmer, this could help you debug your Bash scripts to see if they’re failing at a specific command.
To do this, execute set -x
or set -o xtrace
, which turns on command tracing. While the command itself outputs nothing, subsequent commands will print out right before execution. It is helpful to use brace expansions - a recent Bash feature - in your commands.
If you have a command to remove numbered TXT files, having set
command tracing enabled will show the expanded form of the braces with all the TXT files in that directory.
user@linux:~$ set -x
user@linux:~$ cd /tmp
+ cd /tmp
user@linux:~$ # example of expansion (and comments do not print out in traces)
user@linux:~$ touch {1..5}.txt
+ touch 1.txt 2.txt 3.txt 4.txt 5.txt
user@linux:~$ rm -f *.txt
+ rm -f *.txt
user@linux:~$ # however, asterisk globs do not expand.
Other Useful Options for set
in Bash
The set
command offers numerous other mutually exclusive options for shell operation similar to command tracing, such as not executing the command, changing the command line to an Emacs-style editor, printing backtraces when an error occurs, and so on.
Keep in mind that to turn on an option, you must use set -o <option_name>
and set +o <option_name>
to turn it off.
A complete listing of these options is explained below, with information sourced from the GNU manual for the set built-in.
allexport
- If you have a Bash script that sets environment variables, and you’d like those variables to be available to the current shell and any subsequent subshells,set -a
orset -o allexport
causes those variables to be exported and becomes available to those shell contexts.braceexpand
- brace expansion allows us to expand a constant set of a pair of numbers into a space-delimited string of the range of those numbers. Turning this off will disable brace expansion if you want to have a lower command in the shell evaluate the braces.Emacs
- Enables emacs-based editing on the command line for those familiar with it.errexit
- If a command in a Bash script exits with a non-zero status code, the entire script will stop. It can be prevented by ORing the failing command with a second command that returns a zero exit code to prevent the script from prematurely exiting.errtrace
- A backtrace to the failing command prints out if a program exits with a non-zero status code.hashall
- Keeps track of command locations when they are searched for in the environment PATH. It caches the command paths in one location for faster search and execution.histexpand
- Expands the “!” character in strings as a history substitution feature. If you’re having trouble using the “!” character in strings and don’t do much history substitution work, you can safely turn this off.History
- It’s a crucial feature that allows you to maintain a list of previously executed commands.ignoreeof
- The EOF character (provided by Ctrl-D) typically exits a Bash shell or a command accepting input. Disabling this option results in EOF being ignored, which could break some commands which only accept EOF.Monitor
- Enables job control, causing processes that run in the background to print out their exit code as they complete execution. It’s useful to check on background processes as they finish.noclobber
- Prevents Bash redirection utilities such as>&, <>
from truncating existing files. It’s handy if you’re dealing with real log files while testing and don’t wish to delete existing logs accidentally.noexec
- Prints out commands but doesn’t execute them. If used in a script,noexec
can skip certain commands and execute others by turning them on and off at certain points.noglob
- Prevents the expansion of globs (i.e. *.txt).Notify
- When used withmonitor
,notify
prints out the background job status codes immediately, instead of waiting for the next shell prompt.nounset
- Return an error code if a variable is not set and its value is attempted to be read.onecmd
- Exit after reading and executing the next command.
More options are in the manual above, should you wish to read them. Hopefully, these options can better your Bash programming experience.
If you’d like to read about these options again from the command line, try running the help set
. We also recommend The Linux Documentation Project’s page for more information about the set
command.