What are the differences between shell and environment variables? Where are these variables stored?
shell和环境变量之间有什么区别?这些变量存储在哪里?
4 个解决方案
#1
17
Citing this source,
引用此来源,
Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.
标准UNIX变量分为两类,环境变量和shell变量。从广义上讲,shell变量仅适用于shell的当前实例,用于设置短期工作条件;环境变量具有更深远的意义,登录时设置的那些变量在会话期间有效。按照惯例,环境变量具有UPPER CASE,而shell变量具有小写名称。
To list all environment variables, use printenv
and to list all shell variables, use set
.
要列出所有环境变量,请使用printenv并列出所有shell变量,使用set。
You'll note that the environment variables store more permanent value, e.g.:
您会注意到环境变量存储更多永久值,例如:
HOME=/home/adam
Which changes quite seldom, while the shell variables stores local, temporary, shell-specific values, e.g.:
哪个更改很少,而shell变量存储本地,临时,特定于shell的值,例如:
PWD=/tmp
which changes every time you change your current directory.
每次更改当前目录时都会更改。
For most practical tasks, set environment values by adding export VARIABLE_NAME=VALUE
to your ~/.bashrc file.
对于大多数实际任务,请通过向〜/ .bashrc文件添加导出VARIABLE_NAME = VALUE来设置环境值。
#2
9
Their difference is similar to the difference between private fields and protected fields in a Java class.
它们的区别类似于Java类中私有字段和受保护字段之间的区别。
The private fields of a Java class is only accessible from that Java class. The protected fields of a Java class is accessible from both that Java class and its subclasses.
只能从该Java类访问Java类的私有字段。可以从该Java类及其子类访问Java类的受保护字段。
The shell variables of a shell is only accessible from that shell process. The environment variables exported from that shell is accessible from both that shell process and the sub-processes created from that shell.
shell的shell变量只能从该shell进程访问。从该shell导出的环境变量可以从该shell进程和从该shell创建的子进程访问。
#3
2
For Bash shell:
对于Bash shell:
Shell variables differ from environment variables in different ways:
Shell变量以不同方式与环境变量不同:
♦ A shell variable is specific to the shell itself and is not inherited by child processes. For example, let's say you're running another application from the shell, that application will not inherit the shell variable:
♦shell变量特定于shell本身,不会被子进程继承。例如,假设您正在从shell运行另一个应用程序,该应用程序将不会继承shell变量:
$ SHELL_VAR=xyz
$ firefox
SHELL_VAR
will not be available in the environment of the child process (firefox).
SHELL_VAR在子进程(firefox)的环境中不可用。
♦ In contrast, environment variables of the parent process (the shell here) are inherited by all child processes:
♦相反,父进程的环境变量(此处为shell)由所有子进程继承:
$ export SHELL_VAR=xyz
$ firefox
♦ Both shell and environment variables are local to the shell/process which defined them:
♦shell和环境变量都是定义它们的shell /进程的本地变量:
Environment variables can be persistent, whereas, for shell variables once you exit the session, they're all gone.
环境变量可以是持久的,而对于shell变量,一旦退出会话,它们就会消失。
Note: the above examples only alter the shell that you're working on, in other words, if you logout or start a new shell/terminal you're not going to see the variables that you defined, this is per the principle of process locality.
注意:以上示例仅改变您正在处理的shell,换句话说,如果您注销或启动新的shell /终端,您将不会看到您定义的变量,这是按照进程的原则局部性。
How to make presistent shell variables:
如何创建presistent shell变量:
One way to do that is by modifying the ~/.profile file:
一种方法是修改〜/ .profile文件:
export SHELL_VAR=xyz
This setting is user-specific and not system-wide, for system-wide environment variables, you can add the above line to a .sh file in /etc/profile.d
此设置是特定于用户的,而不是系统范围的,对于系统范围的环境变量,您可以将以上行添加到/etc/profile.d中的.sh文件中。
I highly recommend reading this page: EnvironmentVariables
我强烈建议您阅读此页面:EnvironmentVariables
#4
-1
A shell variable is just a special case of an environment variable. shell variables are inherited from the environment and possibly copied to the environment of children of the shell depending on syntax used: http://www.pixelbeat.org/docs/env.html
shell变量只是环境变量的一个特例。 shell变量从环境继承,并可能根据使用的语法复制到shell的子环境中:http://www.pixelbeat.org/docs/env.html
#1
17
Citing this source,
引用此来源,
Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.
标准UNIX变量分为两类,环境变量和shell变量。从广义上讲,shell变量仅适用于shell的当前实例,用于设置短期工作条件;环境变量具有更深远的意义,登录时设置的那些变量在会话期间有效。按照惯例,环境变量具有UPPER CASE,而shell变量具有小写名称。
To list all environment variables, use printenv
and to list all shell variables, use set
.
要列出所有环境变量,请使用printenv并列出所有shell变量,使用set。
You'll note that the environment variables store more permanent value, e.g.:
您会注意到环境变量存储更多永久值,例如:
HOME=/home/adam
Which changes quite seldom, while the shell variables stores local, temporary, shell-specific values, e.g.:
哪个更改很少,而shell变量存储本地,临时,特定于shell的值,例如:
PWD=/tmp
which changes every time you change your current directory.
每次更改当前目录时都会更改。
For most practical tasks, set environment values by adding export VARIABLE_NAME=VALUE
to your ~/.bashrc file.
对于大多数实际任务,请通过向〜/ .bashrc文件添加导出VARIABLE_NAME = VALUE来设置环境值。
#2
9
Their difference is similar to the difference between private fields and protected fields in a Java class.
它们的区别类似于Java类中私有字段和受保护字段之间的区别。
The private fields of a Java class is only accessible from that Java class. The protected fields of a Java class is accessible from both that Java class and its subclasses.
只能从该Java类访问Java类的私有字段。可以从该Java类及其子类访问Java类的受保护字段。
The shell variables of a shell is only accessible from that shell process. The environment variables exported from that shell is accessible from both that shell process and the sub-processes created from that shell.
shell的shell变量只能从该shell进程访问。从该shell导出的环境变量可以从该shell进程和从该shell创建的子进程访问。
#3
2
For Bash shell:
对于Bash shell:
Shell variables differ from environment variables in different ways:
Shell变量以不同方式与环境变量不同:
♦ A shell variable is specific to the shell itself and is not inherited by child processes. For example, let's say you're running another application from the shell, that application will not inherit the shell variable:
♦shell变量特定于shell本身,不会被子进程继承。例如,假设您正在从shell运行另一个应用程序,该应用程序将不会继承shell变量:
$ SHELL_VAR=xyz
$ firefox
SHELL_VAR
will not be available in the environment of the child process (firefox).
SHELL_VAR在子进程(firefox)的环境中不可用。
♦ In contrast, environment variables of the parent process (the shell here) are inherited by all child processes:
♦相反,父进程的环境变量(此处为shell)由所有子进程继承:
$ export SHELL_VAR=xyz
$ firefox
♦ Both shell and environment variables are local to the shell/process which defined them:
♦shell和环境变量都是定义它们的shell /进程的本地变量:
Environment variables can be persistent, whereas, for shell variables once you exit the session, they're all gone.
环境变量可以是持久的,而对于shell变量,一旦退出会话,它们就会消失。
Note: the above examples only alter the shell that you're working on, in other words, if you logout or start a new shell/terminal you're not going to see the variables that you defined, this is per the principle of process locality.
注意:以上示例仅改变您正在处理的shell,换句话说,如果您注销或启动新的shell /终端,您将不会看到您定义的变量,这是按照进程的原则局部性。
How to make presistent shell variables:
如何创建presistent shell变量:
One way to do that is by modifying the ~/.profile file:
一种方法是修改〜/ .profile文件:
export SHELL_VAR=xyz
This setting is user-specific and not system-wide, for system-wide environment variables, you can add the above line to a .sh file in /etc/profile.d
此设置是特定于用户的,而不是系统范围的,对于系统范围的环境变量,您可以将以上行添加到/etc/profile.d中的.sh文件中。
I highly recommend reading this page: EnvironmentVariables
我强烈建议您阅读此页面:EnvironmentVariables
#4
-1
A shell variable is just a special case of an environment variable. shell variables are inherited from the environment and possibly copied to the environment of children of the shell depending on syntax used: http://www.pixelbeat.org/docs/env.html
shell变量只是环境变量的一个特例。 shell变量从环境继承,并可能根据使用的语法复制到shell的子环境中:http://www.pixelbeat.org/docs/env.html