Parameters and Options

We have by now covered the basics of shell variables, positional parameters, and special parameters. However, we have a couple of more details to finish the topic.

Unsetting Variables

The unset utility, exactly as it sounds, unsets a variable. This is distinct from setting a variable to a null string; as we discussed in the previous module, this distinction affects the behavior of modified parameter expansion. Additionally, as we will see in a moment, unset is necessary to un-export variables.

Exporting Variables

Every shell variable may be marked for export, which means it is added to the environment variables of any invoked commands. Otherwise, a non-exported variable is internal to the shell environment itself and not automatically inherited as an environment variable by child processes.

Tip

Some shell variables are automatically present when the shell is invoked, and are also marked for export. These are listed in SH(1P). For example, PWD and HOME.

The export built-in is used to view exported variables or mark variables for export while optionally setting their values,

$ export x='Hello World!' y='Goodbye World!'
$ export
export x='Hello World!'
export y='Goodbye World!'
<...>

Once a variable is marked for export, it remains marked for export for the duration of the shell session, even if its value is change

$ x=hello!
$ printenv x  # x is not an environment variable
$ export x    # mark x for export
$ printenv x  # x is added to the environment of the printenv command
hello!
$ x=goodbye!  # x remains exported
$ printenv x
goodbye!

The only way to un-export a variable is to first unset it with unset, and then recreate it,

$ unset x
$ printenv x  # x no longer exists
$ x=hello!    # create a new variable, also named x, which is not exported
$ printenv x  # x no is not an environment variable
$ <...>

Readonly variables

Shell variables may be marked with the readonly attribute using the readonly utility. A readonly variable cannot be modified, nor can it be unset with unset.

$ readonly x=0
$ x=1
sh: x: readonly variable

Shell Options

We have already discussed how the set utility can be used to manage the shell positional parameters. The set utility can also be used to manage certain shell options. These options are used infrequently, but can be extremely useful at times.

The -x option and -v option are useful for tracing the execution of a shell script when debugging. The -x option turns on tracing, showing each command as well as its various stages of expansion prior to execution. The -v option turns on verbose output, which prints each line of input as it is read; this can be helpful when sometimes the additional information provided by the -x trace is too verbose.

The -e option causes the shell to exit immediately if any command has a non-zero exit status. This is not used frequently, but has uses in avoiding continuing with dangerous commands if previous ones failed, without having to explicitly check the exit status of every command.

Read SET(1P) for the full list of supported shell options.