Now let's learn how to properly set a shell or environment variable.
Shell variables, by convention, should be kept in all caps.
To set a shell variable, simply set it equal to a value.
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.
To set environmental variables, we use the
$ export TEST_VAR='testing' $ printenv | grep TEST_VAR TEST_VAR=testing
Note that any child processes will inherit the environment variables we set.
To change an environmental variable to a shell variable, use the
$ export -n TEST_VAR $ printenv | grep TEST_VAR # No output since TESTVAR is no longer an environment variable. $ set | grep TEST_VAR TEST_VAR='testing'
To completely unset a variable (or remove it from the session) use the
$ unset TEST_VAR
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.
PATH=$PATH:/path/to/new/file export PATH
Remember to not include spaces! Now that our new
$PATHvariable in set in our .bash_profile, we have to run the file.
To execute either startup file, use the
source command. Or you may use its shortcut, which is just a period (
$ source ~/.bashrc $ . ~/.bashrc
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.
Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price