bash shell 和环境变量的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3341372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between shell and environment variables
提问by sunil
What are the differences between shell and environment variables? Where are these variables stored?
shell 和环境变量有什么区别?这些变量存储在哪里?
回答by Adam Matan
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 实例,用于设置短期工作条件;环境变量具有更深远的意义,登录时设置的变量在会话期间有效。按照惯例,环境变量的名称为大写,而 shell 变量的名称为小写。
To list all environment variables, use printenvand 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=VALUEto your ~/.bashrc file.
对于大多数实际任务,通过添加export VARIABLE_NAME=VALUE到 ~/.bashrc 文件来设置环境值。
回答by twimo
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 创建的子进程访问。
回答by direprobs
For Bash shell:
对于 Bash 外壳:
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_VARwill 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:
如何制作持久化的 shell 变量:
One way to do that is by modifying the ~/.profilefile:
一种方法是修改~/.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 .shfile in /etc/profile.d
此设置是特定于用户的而不是系统范围的,对于系统范围的环境变量,您可以将上述行添加到/etc/profile.d 中的.sh文件中
I highly recommend reading this page: EnvironmentVariables
我强烈建议阅读此页面:EnvironmentVariables
回答by pixelbeat
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

