Linux Environment Variables
Linux

Learn Linux Environment Variables Step-By-Step Easy Guide

On the previous posts we talked about some of the basic Linux commands and today we continue our journey and we will talk about something very important in Linux which is Environment Variables

So what are Environment Variables and where it is and the benefit of knowing it?

Well the bash shell we use to run our commands uses a feature called Environment Variables to store some values which is required by the running programs or scripts from that shell, actually this is a very handy way to store persistent data and make it available for any script or a program when you run it from the shell

There are two types of environment variables in the bash shell:

  • Global variables
  • Local variables

Global variables are visible from the shell session and for any running process that run from the shell

Local variables are visible in the shell that creates them

Our points are:

Global variables

Local variables

Setting Local Variables

Setting Global Variables

Persisting Variables

Default Shell Variables

Locating System Variables

Variable Arrays

 

Global variables

The Linux system sets several global environment variables when you log into your system and they are always CAPITAL LETTERS to differentiate them from normal user environment variables

To see those global variables type printenv command

printenv command

As you can see there are a lot of the global environment variables, to print only one of them just type echo command followed by $VariableName, ex: to print HOME variable type echo $HOME

home variable

Local variables

They are just as important as global environment variables. In fact, the Linux system also defines standard local environment variables for you by default

Unfortunately, there is no command that displays only local environment variables but if you type set command you will see the global and local variables for the shell you are running and available to that shell

Setting Local Environment Variables

You can set your own environment variables directly from the shell once you open the shell you’re allowed to create local variables that are visible within your shell process you just type variableName you want, followed by equal sign and the variable value WITHOUT any spaces make sure of that

mysite=likegeeks

and to print the variable value type

echo $mysite

Sure enough, it prints likegeeks

local environment variables

Ok what if you value not just one work may be 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 echo $mysite

set environment variables

If you forget the single quotation the shell will assume that the second word is another command and gives you an error

As you can see I use lower case for my variable not upper case and this is just it is recommended NOT required this helps 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

Setting Global Environment Variables

To create a global environment you have to create a local environment variable, then export it to the global environment

myvar=’I will do it likegeeks’

echo $myvar

export myvar

global enviroment variables

As you can see I don’t use 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 how to make it persistent?

Persisting Environment Variables

You just edit $HOME/.bashrc and type export myvar=’welcome to likegeeks’ and save the file

print persistent enviroment variable

Removing Environment Variables

This can be done by using unset command

unset enviroment variable

Default Shell Environment Variables

As we know that the system defines some variables for us, one of those variables is PATH variable, this variable hold the paths of the directories that the shell use them to look for commands, each directory separated by colon

 

Setting the PATH Environment Variable

If you want to add a directory for the PATH variable so the shell will search in that directory also for an executable to run when you type a command

You just append the path variable followed by colon and the new directory

add path to enviroment variable

and if you want to persist the path variable you have to edit .bashrc file and add the path like this

persistent path variable

There is a secret trick that many sys admins do here is to add period . to the path variable so the shell will search for an executable in the current directory you are in wherever you are

Update: This is risky in some cases you give the attacker the opportunity to run a malicious script or malware in his current directory so I consider that you know what you are doing

Locating System Environment Variables

There are three ways of starting a bash shell

  • default login shell at login time
  • the interactive shell that is not the login shell
  • non-interactive shell to run a script

 

login shell

When you log into the Linux system, the bash shell starts as a login shell. The login shell looks

For four different startup files to process commands from. The order in which the bash shell

processes the files is

  • /etc/profile
  • $HOME/.bash profile
  • $HOME/.bash login
  • $HOME/.profile

/etc/profile runs on every startup for the system with every user the next 3 files for every specific user you can call them user specific environment variables

Interactive shell

If you start a bash shell without logging into a system like when you go on rescue mode or when you lost your password that’s called interactive shell The interactive shell doesn’t act like the login shell, but it still provides a CLI prompt for you to enter commands

If you start Interactive shell the system will not look for /etc/profile but instead will for .bashrc in your HOME directory

Non-interactive shell

This is the shell that the system starts to execute a shell script by itself and here there is no CLI prompt to worry about

 

Users can customize these files to include environment variables and startup scripts for their own use just edit the specific file and type the variable you want and save

Variable Arrays

One of the neat features of environment variables is that they can be used as arrays which hold multiple values

myvar=(first second third fourth)

Now if you check the value of that array by typing echo you will find it returns the first element only

 

to get a specific element just reference it by its position and the positions starts from zero so to get the third one

echo ${myvar[2]}

to display the entire array type asterisk instead of a number

echo ${mytest[*]}

variable array

you can remove an element of the array using unset

unset mytest[2]

or you can remove the whole array

unset myvar