04. Setting Shell and Environment Variables set, export, source, .

Now let's learn how to properly set a shell or environment variable.

Setting shell variables

Shell variables, by convention, should be kept in all caps.

To set a shell variable, simply set it equal to a value.

$ VARIABLE='value'

It's the little things that matter most

Make sure to not use any spaces between the equals sign. If we do, then the shell will interpret the VARIABLE as a command, and the = as an argument.

Additionally, be sure to use single quotes to prevent expansions.

$ VARIABLE = 'value'
-bash: VARIABLE: command not found

Great! Now let's see if it was successfully set with the set variable, which lists out all shell, environmental, and local variables.

$ set | grep VARIABLE

This will list out all variables, and find any matching variables with VARIABLE in their name.

Additionally, we can check that it's a shell variable and not an environment variable by making sure it's not an output with the printenv command, which only prints out environmental variables.

Setting Environmental Varibles

To set environmental variables, we use the export command.

$ export TEST_VAR='testing'
$ printenv | grep TEST_VAR

Note that any child processes will inherit the environment variables we set.

Changing to an environment variable from a shell variable

To change an environmental variable to a shell variable, use the -n option.

$ export -n TEST_VAR
$ printenv | grep TEST_VAR
# No output since TESTVAR is no longer an environment variable.
$ set | grep TEST_VAR

Unsetting a variable

To completely unset a variable (or remove it from the session) use the unset command.

$ unset TEST_VAR

Example: adding to the $PATH

Remember that the $PATH variable contains a colon-separated list where the shell looks for any executable commands.

To add to our $PATH and have it persist for future sessions, we edit our ~/.bash_profile file, appending a colon then file name at the end.

export PATH

Remember to not include spaces! Now that our new $PATHvariable in set in our .bash_profile, we have to run the file.

Executing a startup file

To execute either startup file, use the source command. Or you may use its shortcut, which is just a period (.).

$ source ~/.bashrc
$ . ~/.bashrc

Conventions for storing variables

Remember, any modifications to the path variable should be placed in the .bash_profile file.

For all other changes, edit .bashrc.

If you're a system admin and need to make changes to everyone, go ahead to edit /etc/profile.

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources