Linux Environment variables are variables that store the information for the shell environment & the shell sessions. These variables are accessed by any scripts or Linux program to get a frequently accessed value.

We have discussed briefly Linux Environment variables, but what exactly is a variable. Most of you might know, but for those who don’t, a variable is used for storing some information like a string or a number or file location. When a variable is referenced in a program or shell script, basically it is requesting the stored value for those variables. Variables allow us to store some frequently accessed data so that we don’t have to write it again and again.

Recommended Read: Important PostgreSQL commands that every beginner should know

Also Read: Bash Scripting-2- Using VARIABLES in Linux shell scripts

Now back to the Linux Environment variable, as mentioned above we also reference them as environment variables. These can be of two types:-

1- Local Variables: These variables are created for a single shell session & are only available until a session is running. Once a session ends, we can’t use them again, these variables are only available for the terminal where they were set & can’t be used on other terminals.

2- Global variables: These are system-wide Linux Environment variables & are available to be used for all shells, terminals & sessions.

Important Linux Environment variables commands

1- Create a local Linux environment variable

To create a local variable, use the following command,

# new_varaiable_name=”test value”

Use this without any spaces.

2- Create an global Linux environment variable

To create a global environment variable, first, create a local variable & then export it,

# new_varaiable_name=”test value”

# export new_varaiable_name

3- To check the list of all Linux Environment variables, use the following command,

# set

4- To get value of only a single variable,

# printenv variable_name

5- Unset an environment variable

To remove a variable from the list of environment variables, we can run the following command,

# unset variable_name

6- using PATH command

PATH command is another important command that we might use, it is basically used to save path for either a program or script. We can set the path a script & next time when we need to execute the script, we need not use the full path & can only run the script directly,

To set the path, use

# PATH=$PATH:/home/test-user/testing.sh

Now to run the script, only have to run

# testing.sh

& the script will run, irrespective of whichever directory we are in. We can also set the path for a directory that might contain the scripts & this will also work the same way,

# PATH=$PATH:/home/test-user

That’s it for our tutorial on Linux Environment Variables. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.