Linux Environment Variables
Linux Environment Variables
Learn about local and global environment variables in Linux, which are used to store values which can be used by scripts from the shell, and how to use them.
Join the DZone community and get the full member experience.Join For Free
Open source vulnerabilities are on the rise. Read here how to tackle them effectively.
In the previous posts, we talked about some of the basic Linux commands, today we continue our journey, and we will talk about something very important in Linux which is Linux Environment Variables.
So what are Environment Variables and what is the benefit of knowing them?
Environment Variables are used to store some values which can be used by scripts from the shell. You are not limited to the shell environment variables, you can create your own.
The bash shell has two types of environment variables:
- Local variables
- Global variables
The local variable is visible in the shell where it was defined only.
The global variable is visible for any running process that runs from the shell.
The Linux system sets some global environment variables when you log into your system and they are always CAPITAL LETTERS to differentiate them from user-defined environment variables.
To see these global variables, type the printenv command:
As you can see, there are a lot of global environment variables, to print only one of them, type echo command followed by $VariableName. Ex: to print the HOME variable, type echo $HOME.
Actually, the Linux system also defines some standard local environment variables for you by default. To view the global and local variables for the shell you are running and available to that shell, type the set command.
setting Local Environment Variables
To declare your own environment variables directly from the shell, type the variableName you want, followed by an equal sign and the variable value WITHOUT any spaces:
And to print the variable value, use the echo command:
Sure enough, it prints likegeeks.
OK, what if your variable is more than one word, maybe a long string; you can put the string between single quotations:
mysite=’likegeeks is a website that offers tech tutorials for geeks’
And if we type
If you forget the single quotation, the shell will assume that the second word is another command and will show an error.
As you can see, I use lower case characters for my variable, not upper case, and this is recommended NOT required. This helps you distinguish your environment variables from the system environment variables.
Once you have set your local variable, it will be visible in the currently running shell scope, and that means if you start another shell window, the variable will not be available in that new window.
Declare Global Environment Variables
To declare a global environment variable, you have to declare a local environment variable then use the export command like this:
myvar='I will do it likegeeks' echo $myvar export myvar
As you can see, I don’t use a dollar sign with the export command, so make sure of that.
But there is something- when I close the shell and open it again, the variable is gone, so how do you make it persistent?
persisting Environment Variables
$HOME/.bashrc and type
export myvar='welcome to likegeeks' and save the file:
Removing Environment Variables
This can be done by using the unset command:
default Environment Variables
As we know, the system defines some variables for us. One of those variables is the PATH variable; this variable holds some paths. These paths are the default paths that the shell uses to look for a command when you type it in the shell.
Modifying the PATH Environment Variable
If you add a folder path to the PATH variable, the shell will look in that folder for any executable to run when you type any command.
Just append the path variable followed by a colon and the new directory like this:
If you want to persist the PATH variable, you have to edit the.bashrc file and add type it like this:
There is a useful but RISKY trick that some sysadmins do, which is adding a period. to the path variable. By doing this, the shell will search for executables in the current directory you are in, wherever you are.
This is relatively risky; you might give the attacker the opportunity to run a malicious script or malware in his current directory, so if you do this trick, you should know what you are doing.
system Environment Variables Paths
You can start a bash shell with one of the following ways:
- Login shell.
- The interactive shell.
- The non-interactive shell.
When you log onto the system, the search for those startup files to process the commands from them:
/etc/profile runs on every startup with every user, the other three files run for every specific user, you can call them user specific environment variables.
When you go to the rescue mode, this is the interactive shell.
If you start an Interactive shell, the system will not look for /etc/profile but instead will look for .bashrc in your HOME directory.
This shell runs by the system itself to run a shell script.
Users can customize the above-mentioned files to include environment variables, just edit the desired file and type the variable you want and save it.
You can store many values as you want in one environment variable, which is called an array.
myvar=(first second third fourth)
Now, if you check the value of that array using the echo command, you will find it returns the first element only.
To get a specific element, just reference it by its position. The positions start from zero, so to get the third one, type it like this:
To display the entire array, type an asterisk instead of a number:
You can remove an element of the array using the unset command:
Or you can remove the whole array:
Published at DZone with permission of Mokhtar Ebrahim , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.