Ever wondered how you get the same terminal experience every time you open a new terminal session in Linux? The terminal pulls the configuration file and sets the environment according to the values in the file and the environment variables. But do you know what even is an environment variable? In this article, we will discuss what environment variables are and how to set appropriate values for them in Linux.
What are Environment Variables in Linux
In Linux, some dynamic values that are needed by the operating system to function properly are set using the environment variables. These variables are essentially placeholders for information used to configure various aspects of the system or applications. With environment variables, we can customize and modify an application or even the operating system to cater to our needs, and thus, unlock the full potential of the system.
How to Check Environment Variables in Linux
Now that you know what are environment variables, let’s see how to view the environment variables stored in the Linux system. Viewing environment variables present in your Linux system is easy, and you just need to run this command to print a list of all environment variables:
printenv | less
![listing all environment variables](https://beebom.com/wp-content/uploads/2023/05/env_vars.png?w=593)
Each entry begins with the variable name, followed by an ‘equals to’ symbol (=) and the assigned value, as shown here:
To view a particular environment variable value, just add the variable name after the printenv command like:
printenv <variable_name>
For example, to view the value for the HOME variable, use this command:
printenv HOME
You can also use the echo command to print any environment variable’s value like:
echo $HOME
Note: Always use a ‘$’ symbol when printing variables with the echo command.
How to Set New Environment Variables in Linux
Before you can create environment variables, let’s first see how to create shell variables and try to understand the difference between the two here.
Create Shell Variables
Just like how you can create variables in any programming language, you can also create shell variables. You can use such variables for various operations but are only valid for your current session only. The syntax to create a shell variable in Linux is:
<variable_name>=<variable_value>
For instance, if you want to create a new variable named ‘TEST_VAR’ with the value ‘test_variable’, use this command:
TEST_VAR='test_variable'
While in other programming languages, generally, we use a space on either side of the ‘=’ symbol, but for shell variables, we do not give any space.
To print the variable value, simply use the echo command as follows:
echo $TEST_VAR
If you are curious to check whether this is an environment variable or not, use this command:
printenv | grep TEST_VAR
You will not get any output from this command, which confirms that this is a plain old shell variable.
Create Environment Variables
To set an environment variable in Linux, you need to use the export command followed by the variable name and its value:
export <variable_name>=<variable_value>
For example, to set the variable ‘TEST_ENV_VAR’ as an environment variable with the value “hello world”, use this command:
export TEST_ENV_VAR="hello world"
To confirm whether the environment variable is created or not, use this command. This time you will get an output, showing you the value assigned to the variable.
printenv | grep TEST_ENV_VAR
How to Set Environment Variables Permanently
When you can create environment variables using the previous method, they are only valid for the current session. The moment you log out of the Linux system or shut it down, these values get erased. If you need to keep the values in the system permanently but don’t want to set environment variables every time you log in to your Linux system, use these steps:
Step 1: Open the .bashrc file
Whenever you open a new terminal session, the Linux distro pulls various configurations, aliases, and options from the .bashrc file, which can be tweaked to suit your needs. Simply open the .bashrc file using the nano command:
sudo nano .bashrc
Step 2: Add the New Environment Variable
Scroll down to the end of the .bashrc file using the down arrow key or press CTRL + END on the keyboard and then add the new entry as follows:
export <variable_name>=<variable_value>
For example, to add a new environment variable named TEST_ENV_VAR with the value “Hello World”, use this line:
export TEST_ENV_VAR="Hello World"
Once you have added the environment variable entry, save and exit out of the editor and the terminal. The variable will get permanently stored and will be available the next time you log in.
Step 3: Check for the Environment Variable
Open a new terminal session, and use this command to check for the environment variable:
printenv | grep <variable_name>
For example, if your environment variable name is TEST_ENV_VAR, use this command. As you can see, you are getting the output even in a new session.
printenv | grep TEST_ENV_VAR
Frequently Asked Questions
Environment variables provide an easy way to customize and configure the Linux system according to our needs. They can also be used to control various applications’ behavior and thus help streamline our workflow.
Yes, you can modify existing environment variables in Linux. Simply use the export command with this syntax: export <environment_variabe_name>=<new_value>
To delete environment variables in Linux, just use the unset command as per this syntax: unset <environment_variable_name>
Can u make a tutorial on how to use windows env vars on wsl system ?
It usually sometimes gives – ‘ bash/r/’ error: