reference
- How to Set Environment Variables in Linux {Step-by-Step Guide} (phoenixnap.com)
conclusion
-
设置永久有效的变量就创建并配置/etc/profile.d 目录下的某个 sh 文件;
-
当然,要想配置在/etc/profile 文件中也有效
-
关于/etc/profile.d 目录下的配置文件(开机运行的脚本文件),和/etc/profile 作用之间的关系和区别,有如下论证
-
bash - What do the scripts in /etc/profile.d do? - Unix & Linux Stack Exchange
- 将配置文件合适的分散到/etc/profile.d 目录中的独立脚本,可以提供维护时的方便(问题定位等等);
- 同时让配置的修改更加灵活
- 若通过/etc/profile 来调用/etc/profile.d 中的脚本,这使得位于不同文件中的脚本和位于同一个文件中的脚本效果一样
-
-
删除永久环境变量就从配置改变量的文件处移除即可
Most Common Environment Variables
PWD
– Current working directory.HOME
– The user’s home directory location.SHELL
– Current shell (bash, zsh, etc.).LOGNAME
– Name of the user.UID
– User’s unique identifier.HOSTNAME
– Device’s hostname on the network.MAIL
– User’s mail directory.EDITOR
– The Linux system default text editor.TEMP
– Directory location for temporary files.
How to Check Environment Variables
View All Environment Variables
-
Use the
printenv
command to view all environment variables. Since there are many variables on the list, use theless
command to control the view:printenv | less
-
The output will show the first page of the list and then allow you to go further by pressing Space to see the next page or Enter to display the next line:
- Exit the view by pressing
Q
.
- Exit the view by pressing
Search a Single Environment Variable
-
To check a single environment variable value, use the following command:
printenv VARIABLE_NAME
-
Alternatively, display the value of a variable by using the
echo
command. The syntax is:-
the
$
signal precede variableecho $[VARIABLE_NAME]
-
Search Specific Environment Variables
-
To find all the variables containing a certain character string, use the grep command:
printenv | grep [VARIABLE_NAME]
-
Another command you can use to check environment variables is
set
.- However, this command will also include local variables, as well as shell variables and shell functions.
-
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [18:24:12] $ set |head '!'=0 '#'=0 '$'=713 '*'=( ) -=569JNRXghikls 0=-zsh '?'=0 @=( ) ARGC=0 BG
-
Find an environment variable in the list that contains all the variables and shell functions by using
set | grep
:set | grep [VARIABLE_NAME]
Note: Take a look at our in-depth guide on how to use the Linux set command to learn more about it.
Set an Environment Variable in Linux
set a shell variable
-
The simplest way to set a variable using the command line is to type its name followed by a value:
[VARIABLE_NAME]=[variable_value]
-
As an example, create a variable called
EXAMPLE
with a text value.- If you type the command correctly, the shell does not provide any output.
-
The
set | grep
command confirms the creation of the variable. However,printenv
does not return any output.
- This is because the variable created in this way is
a shell variable
.
- Another way to confirm this is to type
bash
and start a child shell session. Using theecho
command to search for theEXAMPLE
variable now returns no output:
Note: In a child process, EXAMPLE
is not an existing variable.
How to Export an Environment Variable
- If you want to turn
a shell variable
intoan environment variable
, return to the parent shell andexport
it with the export command:
export [VARIABLE_NAME]
- Use
printenv
to confirm the successful export:
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [19:12:26]
$ printenv test_env
test env
- If you open a child shell session now,
echo
will return the environment variable value:
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [18:42:04]
$ test_env="test env"
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [19:09:00] C:1
$ set|grep test_env
test_env='test env'
# 导出临时变量(使得子进程shell可以继承访问到该变量)
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [19:09:04]
$ export test_env
# cxxu_kali @ cxxuWin11 in /mnt/c/users/cxxu/temp [19:09:14]
$ bash
┌──(cxxu_kali㉿cxxuWin11)-[/mnt/c/users/cxxu/temp]
└─$ echo $test_env
test env
- The environment variable created in this way
disappears after you exit the current shell session
.
Set an Environment Variable in Linux Permanently
set permanent environment variables for a single user
-
To set permanent environment variables for
a single user
, edit the.bashrc
file:sudo nano ~/.bashrc
-
Write a line for each variable you wish to add using the following syntax:
export [VARIABLE_NAME]=[variable_value]
-
Save and exit the file. The changes are applied after you restart the shell.
-
If you want to apply the changes during the current session, use the source command:
source ~/.bashrc
-
set permanent environment variables for all users
-
To set permanent environment variables for all users, create an
.sh
file in the/etc/profile.d
folder:sudo vim /etc/profile.d/[filename].sh
-
The syntax to add variables to the file is the same as with
.bashrc
:
- Save and exit the file. The changes are applied at the next logging in.
How to Unset an Environment Variable
-
To unset an environment variable, use the
unset
command:unset [VARIABLE_NAME]
-
This command permanently removes variables exported through a terminal command.
- Variables stored in configuration files are also removed from the current shell session.
- However, they are set again upon next logging in.
-
To permanently unset a variable you stored in a file, go to the file, and remove the line containing the variable definition.