VIEWING AND MODIFYING ENVIRONMENT VARIABLES
You can view all your default environment variables by entering env into your terminal from any directory, like so:
kali >env XDG_VTNR=7
SSHAGENT_PID=922 XDG_SESSION_ID=2
XDG_GREETER_DATA_DIR=/var/lib/lightdm/data/root GLADE_PIXMAP_PATH=:echo
TERM=xterm SHELL=/bin/bash
snip
USER=root
snip
PATH=/usr/local/sbin :usr/local/bin:/usr/sbin:/sbin/bin
snip
HOME=/root
snip
Environment variables are always uppercase, as in HOME, PATH, SHELL, and so on. These are only the default environment variables that come on your system. A user can also create their own variables, and as you will see, we need a different command to include those in the output.
Viewing All Environment Variables
To view all environment variables, including shell variables, local variables, and shell functions such as any userdefined variables and command aliases, use the set
command. This command will list all environment variables unique to your system, which in most cases will give you an output so long you won’t be able to view it all on a single screen. You can request to view each variable, line by line, in a more accessible fashion using set and piping it to the more command, as follows:
kali >set|more BASH=/bin/bash
BASHOPTS=checkwinsize:cmdlist:complete_fullquote:expand_aliases:extglob...
BASH_ALIASES=() BASH_ARGC=()
WOW! eBook
BASH_ARGV=()
snip
Now the list of variables will fill up one screen, line by line, and then stop. When you press ENTER, the terminal advances to the next line, taking you to the next variable, so you can scroll through by pressing or holding ENTER. As you might recall from Chapter 2, whenever you use the more command for output, you can enter q to quit (or exit) and return to the command prompt.
Filtering for Particular Variables
Although using set with more gives more manageable results than looking through the huge chunk of variable names you get with set alone, it can still be rather tedious if you’re looking for a particular variable. Instead, you can use the filtering command grep to find your variable of interest.
Let’s use the variable HISTSIZE as an example. This variable contains the maximum number of commands your command history file will store. These commands are any ones you’ve previously typed into your command prompt in this session and can be recalled with your up and downarrow keys. Note that HISTSIZE doesn’t store the commands themselves, just the number of them that can be stored.
Pipe your set output with grep to find the HISTSIZE variable, like so:
kali >set |grep HISTSIZE HISTSIZE=1000
As you can see, this command finds the variable HISTSIZE and displays its value. The default value of this variable is probably set to 1000 on your system. This indicates that the terminal will store your last 1,000 commands by default.
Changing Variable Values for a Session
Now let’s see how to change a variable’s value. As noted, the HISTSIZE variable contains the value of the number of commands to store in the history file. Sometimes, you won’t want your system to save past commands—perhaps because you don’t want to leave any evidence of your activity on your own system or a target system. In that case, you can set the HISTSIZE variable to 0 so the system won’t store any of your past commands.
Because this variable has a single value, to change it, you assign it a new value in the familiar way shown in Listing 71.
WOW! eBook
kali >HISTSIZE=0
Listing 71: Changing the value of HISTSIZE
Now, when you try to use the up and downarrow keys to recall your commands, nothing happens because the system no longer stores them. This is stealthy, although it can be inconvenient.
Making Variable Value Changes Permanent
When you change an environment variable, that change only occurs in that particular environment; in this case, that environment is the bash shell session. This means that when you close the terminal, any changes you made are lost, with values set back to their defaults. If you want to make the changes permanent, you need to use the export
command. This command will export the new value from your current environment (the bash shell) to the rest of the system, making it available in every environment until you change and export it again.
Variables are strings, so if you run on the cautious side, it isn’t a bad idea to save the contents of a variable to a text file before you modify it. For example, since we’re about to change the PS1 variable, which controls the information you display in the prompt, first run the following command to save the existing values to a text file in the current user’s home directory:
kali >echo$HISTSIZE> ~/valueofHISTSIZE.txt
This way, you can always undo your changes. If you want to be even more cautious and create a text file with all the current settings, you can save the output of the set
command to a text file with a command like this one:
kali >set>~/valueofALLon01012017.txt
After you’ve changed a variable, as we did in Listing 71, you can make the change permanent by entering export and then the name of the variable you changed, as shown here:
kali >exportHISTSIZE
Now the HISTSIZE variable will still be set to 0 when you leave this environment and enter
WOW! eBook
another environment. If you want to reset the HISTSIZE variable to 1,000, simply enter this:
kali >HISTSIZE=1000 kali >exportHISTSIZE
This code snippet will set your HISTSIZE variable’s value to 1,000 and export it to all your environments.