Learn Linux: How to set environmental variables and what are they?
Environmental variables are little nodes of data that can be accessed within the Linux shell session. If you've ever programmed, you'll know that you can assign data to a variable and then use that variable throughout your code to save from having to type out all that data. Well it's the same for Environmental Variables, albeit with a few slight differences.
Note: Check out the cheatsheet version on Environmental variables if you're in a hurry!
First, let's have a look at how you can access your own system's environmental variables.
When you start up a new SSH session, your system automatically loads all the environmental variables into that session. Once loaded, processes from within the shell session can now use the data in these variables to help load up other modules, runs scripts or perform actions on your server.
To see your environmental variables, type the following command:
env
You should now see a list of variables outputted on your systemÂ
You can see that each variable works by assigning a key name to a value. So for example, the variable USER, contains the value of the current logged in user (test_user). We can see the value of a variable by using the echo command.
echo $USER
You can see we add a $ sign to denote that we want to access the variable.
COMMON ENVIRONMENTAL VARIABLES
Variable Name | Value |
---|---|
USER | The current logged in user. |
PATH | a list of absolute paths that are used to search for executables when the user enters the command in the terminal. E.g. if the user typed apachectl, it would search the absolute paths listed in the PATH variable for the program apachectl. |
PWD | The currenty directory the user is in |
OLDPWD | The previous directory the user was in. |
HOME | The home directory of the currently logged in user |
SHELL | The type of shell that the user is using |
LS_COLORS | Used to color various file types when viewing system files through the shell. This is why folders appear in a different color to that of regular files. |
Creating our own Environmental Variables
It's very easy to create your own variables. In the shell, type out your variable in the following format and hit enter. Let's create an example:
export SECRETCODE=128349
Remember to not leave any whitespace between the variable name, the equal sign and the value as it won't work. Now if you type:
echo $SECRETCODE
It should output 128349. There are a few pointers you should note:
- When you create a variable by just typing the export command into the terminal window, it is only accessible for the current shell session. So if you exit the shell now, log back in and try to echo out the variable, no value you will be displayed. To make persistant environmental variables, read below.
- If you omit the export command, the variable will still be created but it will no longer be an environmental variable, but a shell variable. Meaning that the variable won't be accessible from child processes and can only be used within that current shell.
- When assigning the value to a variable, you must enclose the value with single quotes if it contains white space. E.g. MYVAR='hello world'. If the variable doesn't, you can just add it like MYVAR=12345
If you want to unset the variable, simply use:
unset VARIABLENAME
and it'll no longer be available to use.
Setting up persistant Environmental Variables
Sometimes you'll want your variables to persist and be accessible all the time. In order to do that we need to modify the shell startup script which is used to define all the environmental variables. The file you'll need to edit is:
/etc/profile
In this file, you want to define your environmental variable. I would recommend appending it to the bottom of the file so as to not mess up any of the default settings. Simply type:
export VARIABLENAME=value
Save the file. Now when you login, that variable will be available for you to use.
Footnotes
There are a few other scenarios in which you may need the environmental variable to be available. In the above scenario, we have defined the variable for a logged-in user using an interactive terminal. However, there are also:
- Non-logged in shells
- non-interactive shells
Since a logged-in shell is one where the user authenticates themselves and is logged in, a non-logged in shell is one where there is no authentication used. An example is when you spawn a new shell from within your own (no authentication is needed).
An interactive shell is a shell which is attached to a terminal, whereas a non-interactive shell is one that isn't. For example, a shell running a script is always considered a non-interactive shell.
In order to make sure your environmental variables can be accessed through all these different types of shells, you'll need to add your environmental variables to the following scripts:
non-login interactive shell:
nano ~/.bashrc
non-login non-interactive shell:
the contents on $BASH_ENV (if it exists)
Hopefully this will give you a small introduction into the world of environmental variables. They are particularly useful when creating shell scripts that need to reference some data.
Leave A Comment