Updated April 17, 2023
Introduction to Linux Environment Variables
In the Linux ecosystem, the environment variable can be set as a global or a local that are used to execute the shell, bash or any application-level jobs. In other words, we can say that the Linux or UNIX environment variable is nothing but a name and the same name is holding some value or path.
The environment variable is providing the functionality to the end-user, how the application job will run on the Linux environment, and the custom behavior of the system. There are different ways, we can categories the environment variable i.e. global and local variable, or environment and shell variable.
Syntax of Environment Variables
Below is the syntax for Linux Environment Variables:
1. Set Environment Variable
SET VARIABLE METHOD [ ENVIRONMENT NAME ] [ PATH ]
- SET VARIABLE METHOD: We can use different environment methods to set the environment variables. It will accept the two different arguments like environment name and path. Accordingly the input, it will export the environment variable.
- ENVIRONMENT NAME: We can provide different environment names that are compatible with the environment variables.
- PATH: We can pass the path or command to the environment variable.
2. Call Environment Variable
echo $[ ENVIRONMENT NAME ]
- echo: we need to use the echo command to call or check the specific environment variables.
- ENVIRONMENT NAME: we need to pass the environment variable name in the echo command with the “$” sign.
How does Linux Environment Variables Command work?
The environment variables are the variables that are available on the system level. The same variable will be useful for shell or application-level jobs. There are different ways; we can use the environment variables in the Linux environment like env, printenv, set, unset, export, etc.
- env: The env command allows the end-user to run the program or job in the custom environment. While running the job, it will not be impacting or modifying the current environment. When are using the env command without arguments, then it will print the list of available variable options in the system.
- set: The set command is useful to set the environment variables n the Linux machine. When are using the set command without any arguments, then it will print the list of the available variable option, shell variables and the shell functions in the system.
- unset: The unset command is helpful to delete the shell as well as the environment variables.
- printenv: The printenv command is useful to print all the environment variables of the system.
- export: The export command is helpful to sets the environment variables. But the variable is temporary if the session will log out or close then the scope of the variable will vanish.
Examples to Implement Linux Environment Variables Command
Below are the examples mentioned :
Example #1 – VariableDisplay Command
In the Linux environment, we have the functionality to print all the system environment variables. The scope of the variables is global.
Code:
printenv
Explanation: We have used the “printenv” command in the below screenshot. It will print all the defaultenvironment variables of the system. We haven’t used any options with it. We can use the same variables in any application or shell jobs.
Output :
Example #2 – Call Specific Variable
As per the requirement, we can the specific environment variable from the number of environment variables available in the system.
Code:
printenv SSH_CLIENT
Explanation: As per the above command, we are using the specific environment variablei.e. “SSH_CLIENT”. It will only display the value associated with the “SSH_CLIENT” environment variable.
Output:
Example #3 – Setup Environment Variable
In different ways, we can define our own or user environment variables.
Note: The scope of the variable will only for the login session unless and until the user environment variables will be hardcoded in the “environment” file.
Code:
export log='ls /var/log/'
$log
Explanation: As per the below screenshot 1 (a), there is no scope of the “log” variable. With the help of “export” command, we are creating the environment variable i.e. “log”. Once the “log” variable will export. We are able to use the “log” variable (refer screenshot 1 (b)).
Output:
Example #4 – Update “Environment” File
In the Linux environment, we are having the functionality to hard code the user define an environment variable. Once it will be hardcoded then the scope of the user-defined variable will be global.
Code:
vim /etc/environment
cat /etc/environment
Explanation: As per the above command, we are editing the “/etc/environment” file. When we are defining the user define the environment variable. Then the scope the variable will only limit to the login session. To keep the variable scope global. We need to do the variable entry in “/etc/environment” file and edit the variable and its path like “log=’ls /var/log/’”
Output:
Example #5 – Delete Variables
In the Linux ecosystem, we are able to delete the environment variables. We can also delete the default system variables, as well as the users, define variables.
Code:
unset log
$log
Explanation: As per the below screenshot 3 (a), we have set the environment variable “log” via “export” command. We are able to use the same “log” variable. But now, we need to delete the same “log” environment variable from the Linux environment. We can use the “unset” command to delete the environment variable. Once we will trigger the above command. The scope of the variable will vanish and no longer available in the environment (refer screenshot 3 (b)).
Output:
Conclusion
We have seen the uncut concept of “Linux Environment Variables command” with the proper example, explanation, and command with different outputs. The environment variables are very essential to export the path or set the specific values on the Linux environment. It will import for the third party software or application to access the Linux environment and execute their jobs.
Recommended Articles
We hope that this EDUCBA information on “Linux Environment Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.