Mastering set and unset Commands in Linux: Managing Shell Variables and Functions
The set command displays a list of all local variables, environment variables, and shell functions.
Some set command options change Bash's behavior, namely:
- -C Prevents a program's output using `>', `>&' and `<>' from overwriting files. Does the same as the -o noclobber option
- -n Reads commands but does not execute them. Useful for checking scripts. Does the same as the -o noexec option
- -P Prohibits the shell from following symPara não permitir que um arquivo seja regravado com o condutor ">"bolic links. Does the same as the -o physical option
- -a Marks modified or created variables for export. Does the same as the -o allexport option
- -o history Enables saving the history of typed commands.
- -x Shows the typed command and its result
- -m Enables Job Control
- -e Exits the shell as soon as the executed command finishes successfully
- -n Reads commands but does not execute them. Does the same as the -o noexec option
- -f Disables the use of wildcards * and ?. Does the same as the -o noglob option
When using set options, the + symbol can be used to disable the options.
This command, in addition to listing all variables, can also change Bash's behavior.
See the examples:
List variables:
$ set
BASH=/bin/bash
BASH_VERSION='4.2.46(2)-release'
HISTCONTROL=ignoredups
HISTFILE=/home/ec2-user/.bash_history
No Clobber (do not overwrite)
To prevent a file from being overwritten with the ">" redirector"
$ set -C$ cat teste.c > texto-bash: texto: cannot overwrite existing file
Print what is typed
To print the typed command and its result:
$ set -x$ echo $TERM+ echo xterm-256colorxterm-256color
To disable this, we can use the option with +x:
$ set +x
No History
To prevent typed commands from being saved to history, we can use the +o history option:
$ set +o history
To resume saving typed commands to history:
$ set -o history
Note the trick: some options are enabled with "-" and others are disabled with "+" before the option.
No glob (no wildcards)
To prevent Bash from using 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: não é possível acessar /bin/zip*: No such file or directory
It's almost certain that the -C (noclobber) and -f (noglob) options will be exam questions on the LPI.
It's important to know that the options that change the set command's behavior are not permanent if they are not in some Bash initialization script. Some distributions customize shell behavior with the set command in /etc/profile or /etc/bashrc files.
Unset command
The unset command removes an environment variable from memory.
Ex.:
$ LIVRO="Certificação Linux"
$ echo $LIVRO
Certificação Linux
$ unset LIVRO
$ echo $LIVRO
Printenv command
The printenv command prints environment variables.
As a parameter, it can receive the name of a variable.
Example:
$ printenv PATH/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/bin
If no parameter is provided, it prints all environment variables.
Only variables exported with the export command are printed by printenv.
See the example:
$ BOOK="Certificação CompTIA Linux"$ printenv BOOK
If the BOOK variable is exported:
$ export BOOK$ printenv BOOKCertificação CompTIA Linux
Export command
The export command is used to export created variables to all Bash child processes (programs).
This export is to a special shared memory area between Bash and the programs the Shell executes.
If a variable or function is not specified as a parameter, it shows all 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 executed in the same terminal or Shell session.
Example of directly creating a variable and exporting it at the same time:
$ export LIVRO="Certificação Linux"
$ export
declare -x LIVRO="Certificação Linux"( ... )
To illustrate this well, let's create a variable with my name:
$ NOME="Uirá Ribeiro"
In the shell itself, you can see its content with the echo command:
$ echo $NOMEUirá Ribeiro
Note that, as long as we don't export the NOME 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 "Meu nome é $NOME"
To execute the meunome.sh script, you need to change the permissions for execution:
$ chmod +x meunome.sh
When executing meunome.sh, it will not be able to read the content of the NOME variable that was previously created in the shell:
$ ./meunome.shMeu nome é
But if the NOME variable is exported:
$ export NOME
If we run meunome.sh again:
$ ./meunome.shMeu nome é Uirá Ribeiro
The export command made it possible for the NOME variable to be available to all commands and programs executed by the shell that is running, that is, to all child processes of this shell.
To make it very clear, another shell executed in another terminal or session will not be able to read the NOME variable, because it is not in any Bash profile or startup script.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.