Skip to content

Set/unset command on Linux (Shell variables and functions)

The set command provides a list of all local variables, environmental variables, and shell functions.

Some set command options change the behavior of bash, namely:

  • -C Prevent the output of a program using >', >&’ and `’ from rewriting files. It does the same as the -o noclobber - -n option. It reads the commands but does not execute them. Useful for checking scripts. It does the same as the -o noexec
  • -P option. Prohibit the shell from following links if to not allow a file to be rewritten with the symbolic “>” conductors. It does the same as the -o physical
  • -a option Marks variables modified or created for export. It does the same as the -o allexport
  • -o history option. Enables saving the history of typed commands.
  • -x Shows the entered command and its result
  • -m Enables Task Control (Jobs)
  • -e Exits the shell as soon as the executed command successfully finishes
  • -n Reads the commands but does not execute them. It does the same as the -noexec - -f option Disables the use of wildcards * and?. Does the same as the -noglob option

When using the set options, the + symbol can be used to disable the options.

This command, in addition to being used to list all variables, can alter the behavior of bash.

See the examples:

List the variables:

$set
Bash=/bin/bash
BASH_VERSION='4.2.46 (2) -release'
HistControl=ignoredUps
HistFile=/home/ec2-user/.bash_history

In Clobber (don’t overwrite)

To not allow a file to be rewritten with the driver “>“”

$ set -C $ cat teste.c > text -bash: text: cannot overwrite existing file

To print the entered command and its result:

$ set -x $ echo $TERM + echo xterm-256color xterm-256color

To disable this, we can use the option with the +x:

$ set +x

Without History

To prevent the entered commands from being recorded in history, we can use the +o history option:

$ set +o history

To re-record the commands entered in history:

$ set -o history

Notice the catch that some options are activated with the “-” and others deactivated with the “+” before the option.

On the globe (without jokers)

To prevent Bash from making use of wildcards, you can use the -f or -o noglob option:

$ ls /bin/zip * /bin/zip /bin/zipcloak /bin/zipgrep /bin/zipinfo /bin/zipnote /bin/zipsplit $ set -f $ ls /bin/zip * ls: unable to access /bin/zip *: No such file or directory

It is almost certain that the options -C (noclobber) and -f (noglob) will be test questions in the LPI.

It is important that you know that the options that change the behavior of the set command are not permanent if they are not in a bash startup script. Some distributions customize shell behavior with the set command in the /etc/profile or /etc/bashrc files.

Unset command

The unset command deletes an environmental variable from memory.

E.g.:

$ book="Linux certification”
$ echo $BOOK
Linux Certification
$ unset BOOK
$ echo $BOOK

printenv command

The printenv command prints the environmental variables.

As a parameter, it can take the name of a variable.

Example:

$ printenv PATH /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/bin

If no parameters are entered, print all environmental variables.

Only variables exported with the export command are printed by printenv.

See the example:

$ BOOK="CompTIA Linux Certification” $ printenv BOOK

If the BOOK variable is exported:

$ export BOOK $ printenv BOOK CompTIA Linux Certification

Export command

The export command is used to export variables created for all Bash child processes (programs).

This export is to a special memory area shared between Bash and the programs that the Shell executes.

If a variable or function is not entered as a parameter, it shows all the exported variables. It can also be used to create a variable and export it at the same time.

A variable created in bash without being exported cannot be read by any process or program running in the same terminal or Shell session.

Example of how to directly create a variable and export at the same time:

$ export book="Linux certification”
$ export
declare -x book="Linux certification”

(…) To illustrate this well, let’s create a variable with my name:

$ name="Uira Ribeiro”

In the shell itself, you can view its content with the echo command:

$ echo $NAME Uira Ribeiro

Note that until we export the NAME variable with the export command, it will not be available to any program or shell script executed.

For example, if we create a script called meunome.sh with the following content:

#! /bin/bash

echo “My name is $NAME” To run the meunome.sh script, you must change the permissions for execution:

$ chmod +x meunome.sh

When running meunome.sh, it will not be able to read the contents of the variable NAME that was created earlier in the shell:

$. /meunome.sh

My name is But if the NAME variable is exported:

$ export NAME

If we rerun meunome.sh:

$. /meunome.sh

My name is Uira Ribeiro The export command made it possible for the NAME variable to be available for all commands and programs executed by the shell that is running, that is, for all child processes of this shell.

To be very clear, another shell running in another terminal or session will not be able to read the NAME variable, because it is not in any Bash profile or load script.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share