- What is a Linux environment variable?
- Scope of an Environment Variable in Linux
- Most Common Environment Variables
- How does Linux Environment Variables Command work?
- How to Check Environment Variables
- Set an Environment Variable in Linux
- How to Export an Environment Variable
What is a Linux environment variable?
Linux environment variable is dynamic values found within the system that helps applications launch using the shell or their subshells. Most of the variables have a name and a value. They enable users to change how the system works and the application's behavior. For example, if you want to store information on your favorite browser, you can use the variables to set the path, keyboard settings, etc.
It uses the following syntax :
SET VARIABLE METHOD [ ENVIRONMENT NAME ] [ PATH ]
The method varies typically, and users can use different methods. You must ensure the environment name is compatible with the given variables. Then in the last argument, you bring the path.
If you want to call the environment variable, you use the command below:
echo $[ ENVIRONMENT NAME ]
where echo checks the environment variables and the second command you input your environment name.
Become a LinuxCertified professional by learning this HKR Linux Training !
Linux Certification Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
Scope of an Environment Variable in Linux
The scope is the region users can use to access the environment variables. The scope can also be on how the Scope got defined. There are two types of scope, namely:
- Global environment variables - In this type of environment, you can access it from anywhere, provided the environment exists in the terminal. The terminal supports all the scripts, processes, and programs that run in the set environment.
- Local variable - This variable is defined by the terminal, but not all programs and processes can have access to it. You can only access the variables using since it's where the variable gets declared.
Most Common Environment Variables
Some of the common environment variables commonly used are:
- PATH - It stores all the path lists users will look for when executing different commands.
- MAIL - It shows the location of the current user's email (where it gets stored).
- LOGNAME - It shows the current user's name.
- USER - It shows the username.
- EDITOR - it is the user’s file editor, and It is the editor that opens when you require it.
- UID - It handles the unique ID of the users.
- TERM - it shows the current emulation of the terminal.
- HOME - It shows the home directory.
- MANPATH - It shows the directories for all the manual pages.
- HOSTNAME - It shows the name of the host.
- PS1 - Used as the default prompt in the bash terminal.
- SHELL - It shows the user's shell, which can be bash or zsh.
- LANG - shows the current LANG settings.
- TZ - It shows the time zone.
- PWD - It shows the working directory.
- OSTYPE - It shows the operating system type.
- HISTFILESIZE - It shows the highest number of lines in file history.
Want to know more about Linux,visit here Linux Tutorial !
Subscribe to our YouTube channel to get new updates..!
How does Linux Environment Variables Command work?
Let us look at how the environment variables work. We will explain some common variables: Set, Export, Unset, and Set, among others.
Env
They are for setting the local environment variables. If the variable already exists, it usually gets replaced by the variables under the current session. It lists all the environment variables when the command gets executed without including an argument.
Set
We use the set command to set the required environment variables. When you use it without arguments, it returns a list of all the available variables, shell functions, and variables in the system.
Unset. We use this command to delete all the environment variables, including the shell.
Printenv
When you use the variable's name as an argument, it only displays the values that match the variable. If the variable has no arguments, it prints all the variables, whose arrangement is normally one per line. If you have many variables in your list and you want to display a few variables, you use the less variable, i.e., printenv | less. When you want to find variables with certain characters, you use the grep command.
i.e., printenv | grep [VARIABLE_NAME].
Export. It helps in setting the environment variables. It helps change shell variables into environment variables and then return to the parent shell.
How to Check Environment Variables
There are several methods one can use to check the environment variables. Users can search single or specific variables, view all the variables, export and set variables, etc. The following are ways of checking the environment variables.
View All Environment Variables
We use the printenv command to view all the environment variables. The syntax of the command is as below:
printenv
Search a Single Environment Variable
We use the printenv command to search for a single variable as below:
printenv VARIABLE NAME
You can also use the echo command to display the value of the environment variable as follows:
echo $[VARIABLE_NAME]
Search Specific Environment Variables
When you want to get specific variables, we use the grep command as an argument together with printenv. It is commonly used when you want to find variables that have a certain character in their strings. The command is as follows:
printenv | grep [VARIABLE_NAME]
We can also use the set command to check specific variables. The only difference is that the command includes all the shell functions, variables, and local variables. For you to get the list, you add grep as an argument as follows:
set | grep [VARIABLE_NAME]
Set an Environment Variable in Linux
You can choose to set environment variables either between all the users or the current users. When you want to change for one user, we edit the .bashrc file by typing the command below:
sudo nano ~/.bashrc
We then assign each variable using the command below:
export [VARIABLE_NAME]=[variable_value]
To apply the changes you use:
source ~/.bashrc
When you want to set the environment variables for all users, we create a new .sh file under the /etc/profile.d. We use the command below:
sudo nano /etc/profile.d/[filename].sh
Save to apply the changes.
Top 30 frequently asked Linux Interview Questions !
How to Export an Environment Variable
We use the export command to export any environment variable. You go to shell and type the command followed by the variable name i.e
export [VARIABLE_NAME]
You can use the printenv command to confirm if the export was successful. When you exit the shell, most of the environment variables disappear.
How to Set an Environment Variable in Linux Permanently
You have to ensure you have access to the terminal and your system has Linux installed. We use the set command to set the environment variables. The command has options that show the settings found under the Bash environment.
It also has positional arguments, usually in order, i.e., $3, $4. If you don't specify the arguments, the command will return all the shell variables.
How to Unset an Environment Variable
We use the unset command when unsetting environment variables. We type the command name followed by the variable name. It removes all the variables that got exported using the terminal. To permanently unset all the variables stored in a file, locate the file and remove the lines containing the variable definitions. We use the syntax below:
unset [VARIABLE_NAME]
Final Thought
The article has covered some of the concepts of Linux environment variables. They are essential, and every IT professional should learn how to use them as they will benefit them in some aspects of their careers. Sometimes you have to understand how the variables work and their effect on all the applications and programs. They also help in software configuration. If the applications may need new variables, you know what to do.
Related blogs:
About Author
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.
Upcoming Linux Certification Training Online classes
Batch starts on 25th Nov 2024 |
|
||
Batch starts on 29th Nov 2024 |
|
||
Batch starts on 3rd Dec 2024 |
|
FAQ's
You can see all the environment variables using the " env " or “printenv” commands.
The environment variables get stored in the /etc/environment directory.
The path variable is an environment variable with lists of ordered paths, and Linux will use them to search when running different commands.
There are two types of variables: shell and environment variables.
We declare variables in Linux by giving them a name and a value using the = sign. When printing the variable, there is a dollar sign($) at the beginning of every variable. The variable's value can be a string, characters, numbers, or other special characters. You have to ensure there is no space between the variable name, its value, and the assignment operator. For example, echo $total.
The maximum length of a Linux environment variable can be 131072 characters, around 128KiB.
The environment variables persist when using Linux, especially in the child processes.
They help in changing the environment.