热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

定义一个带有或没有导出的变量。-Definingavariablewithorwithoutexport

Whatisexportfor?出口是干什么用的?Whatisthedifferencebetween:的区别是什么:exportnamevalueand和n

What is export for?

出口是干什么用的?

What is the difference between:

的区别是什么:

export name=value

and

name=value

14 个解决方案

#1


727  

export makes the variable available to sub-processes.

导出使子进程可用变量。

That is,

也就是说,

export name=value

means that the variable name is available to any process you run from that shell process. If you want a process to make use of this variable, use export, and run the process from that shell.

意味着可以从该shell进程中运行的任何进程使用变量名。如果您想要使用此变量的过程,请使用export,并从该shell运行流程。

name=value

means the variable scope is restricted to the shell, and is not available to any other process. You would use this for (say) loop variables, temporary variables etc.

意味着变量作用域仅限于shell,并且不能用于任何其他进程。你可以用这个for(比方说)循环变量,临时变量等等。

It's important to note that exporting a variable doesn't make it available to parent processes. That is, specifying and exporting a variable in a spawned process doesn't make it available in the process that launched it.

需要注意的是,导出一个变量并不能使其对父进程可用。也就是说,在派生的进程中指定和导出一个变量,在启动它的过程中并不能使它可用。

#2


177  

To illustrate what the other answers are saying:

为了说明其他的答案是什么

al$ foo="Hello, World"
al$ echo $foo
Hello, World
al$ bar="Goodbye"
al$ export foo
al$ bash
bash-3.2$ echo $foo
Hello, World
bash-3.2$ echo $bar

bash-3.2$ 

#3


60  

Others have answered that export makes the variable available to subshells, and that is correct but merely a side effect. When you export a variable, it puts that variable in the environment of the current shell (ie the shell calls putenv(3) or setenv(3)). The environment of a process is inherited across exec, making the variable visible in subshells.

另一些人回答说,出口使这个变量可以被亚壳所使用,这是正确的,但仅仅是一个副作用。当您导出一个变量时,它将该变量置于当前shell的环境中(即shell调用putenv(3)或setenv(3))。进程的环境在整个exec中被继承,使变量在子shell中可见。

#4


37  

It has been said that it's not necessary to export in bash when spawning subshells, while others said the exact opposite. It is important to note the difference between subshells (those that are created by (), ``, $() or loops) and subprocesses (processes that are invoked by name, for example a literal bash appearing in your script). Subshells will have access to all variables from the parent, regardless of their exported state. Subprocesses on the other hand will only see the exported variables. What is common in these two constructs is that neither can pass variables back to the parent shell.

有人说,当生成子shell时,不需要在bash中导出,而另一些则说正好相反。重要的是要注意子shell(由()、'、$()或循环)和子进程(由名称调用的进程)和子进程之间的区别(例如,在您的脚本中出现了一个字面的bash)。子shell将访问来自父类的所有变量,而不考虑它们的输出状态。另一方面,子进程只会看到导出的变量。这两种结构中常见的是,它们都不能将变量传递回父shell。

$ noexport=noexport; export export=export; (echo subshell: $noexport $export; subshell=subshell); bash -c 'echo subprocess: $noexport $export; subprocess=subprocess'; echo parent: $subshell $subprocess
subshell: noexport export
subprocess: export
parent:

There is one more source of confusion: some think that 'forked' subprocesses are the ones that don't see non-exported variables. Usually fork()s are immediately followed by exec()s, and that's why it would seem that the fork() is the thing to look for, while in fact it's the exec(). You can run commands without fork()ing first with the exec command, and processes started by this method will also have no access to unexported variables:

还有一种混乱的来源:一些人认为“分叉”的子流程是那些看不到非导出变量的子过程。通常,fork()会紧跟exec(),这就是为什么fork()看起来是要查找的东西,而实际上是exec()。您可以使用exec命令来运行没有fork()的命令,并且该方法启动的进程也无法访问未导出的变量:

$ noexport=noexport; export export=export; exec bash -c 'echo execd process: $noexport $export; execd=execd'; echo parent: $execd
execd process: export

Note that we don't see the parent: line this time, because we have replaced the parent shell with the exec command, so there's nothing left to execute that command.

注意,我们没有看到父类:这一次,因为我们已经用exec命令替换了父shell,所以没有什么可以执行这个命令。

#5


26  

export NAME=value for settings and variables that have meaning to a subprocess.

输出名称=对子进程有意义的设置和变量的值。

NAME=value for temporary or loop variables private to the current shell process.

NAME=对当前shell进程私有的临时变量或循环变量的值。

In more detail, export marks the variable name in the environment that copies to a subprocesses and their subprocesses upon creation. No name or value is ever copied back from the subprocess.

更详细地说,导出将环境中的变量名标记为在创建时复制到子进程及其子进程的环境。任何名称或值都不会从子流程中复制回来。

  • A common error is to place a space around the equal sign:

    一个常见的错误是在等号的周围放置一个空格:

    $ export FOO = "bar"  
    bash: export: `=': not a valid identifier
    
  • Only the exported variable (B) is seen by the subprocess:

    只有导出的变量(B)被子流程看到:

    $ A="Alice"; export B="Bob"; echo "echo A is \$A. B is \$B" | bash
    A is . B is Bob
    
  • Changes in the subprocess do not change the main shell:

    子进程的更改不会改变主shell:

    $ export B="Bob"; echo 'B="Banana"' | bash; echo $B
    Bob
    
  • Variables marked for export have values copied when the subprocess is created:

    在创建子流程时,标记为导出的变量具有复制的值:

    $ export B="Bob"; echo '(sleep 30; echo "Subprocess 1 has B=$B")' | bash &
    [1] 3306
    $ B="Banana"; echo '(sleep 30; echo "Subprocess 2 has B=$B")' | bash 
    Subprocess 1 has B=Bob
    Subprocess 2 has B=Banana
    [1]+  Done         echo '(sleep 30; echo "Subprocess 1 has B=$B")' | bash
    
  • Only exported variables become part of the environment (man environ):

    只有出口变量成为环境的一部分(man环境):

     $ ALICE="Alice"; export BOB="Bob"; env | grep "ALICE\|BOB"
     BOB=Bob
    

So, now it should be as clear as is the summer's sun! Thanks to Brain Agnew, alexp, and William Prusell.

所以,现在应该和夏天的太阳一样清楚了!感谢Brain Agnew, alexp和William Prusell。

#6


8  

export will make the variable available to all shells forked from the current shell.

导出将使所有的shell都可以从当前shell中获取变量。

#7


8  

It should be noted that you can export a variable and later change the value. The variable's changed value will be available to child processes. Once export has been set for a variable you must do export -n to remove the property.

应该注意的是,您可以导出一个变量,然后更改该值。变量的更改值将用于子进程。一旦为变量设置了导出,就必须执行export -n 来删除该属性。

$ K=1
$ export K
$ K=2
$ bash -c 'echo ${K-unset}'
2
$ export -n K
$ bash -c 'echo ${K-unset}'
unset

#8


5  

As you might already know, UNIX allows processes to have a set of environment variables, which are key/value pairs, both key and value being strings. Operating system is responsible for keeping these pairs for each process separately.

正如您可能已经知道的,UNIX允许进程拥有一组环境变量,这些变量是键/值对,键值和值都是字符串。操作系统负责将这些对分别保存在每个进程中。

Program can access its environment variables through this UNIX API:

程序可以通过这个UNIX API访问其环境变量:

  • char *getenv(const char *name);
  • char * getenv(const char *名称);
  • int setenv(const char *name, const char *value, int override);
  • int setenv(const char *name, const char *value, int override);
  • int unsetenv(const char *name);
  • int unsetenv(const char *名称);

Processes also inherit environment variables from parent processes. Operating system is responsible for creating a copy of all "envars" at the moment the child process is created.

进程也从父进程继承环境变量。在创建子进程时,操作系统负责创建所有“envars”的副本。

Bash, among other shells, is capable of setting its environment variables on user request. This is what export exists for.

Bash在其他shell中可以根据用户请求设置环境变量。这就是导出的目的。

export is a Bash command to set environment variable for Bash. All variables set with this command would be inherited by all processes that this Bash would create.

导出是为Bash设置环境变量的Bash命令。使用此命令设置的所有变量都将由该Bash创建的所有进程继承。

More on Environment in Bash

更多关于Bash环境的内容。

Another kind of variable in Bash is internal variable. Since Bash is not just interactive shell, it is in fact a script interpreter, as any other interpreter (e.g. Python) it is capable of keeping its own set of variables. It should be mentioned that Bash (unlike Python) supports only string variables.

Bash中的另一种变量是内部变量。由于Bash不仅仅是交互式shell,它实际上是一个脚本解释器,就像任何其他解释器(例如Python)一样,它能够保留自己的一组变量。应该提到的是Bash(不像Python)只支持字符串变量。

Notation for defining Bash variables is name=value. These variables stay inside Bash and have nothing to do with environment variables kept by operating system.

定义Bash变量的符号是name=值。这些变量驻留在Bash中,与操作系统保存的环境变量无关。

More on Shell Parameters (including variables)

更多关于Shell参数(包括变量)

Also worth noting that, according to Bash reference manual:

同样值得注意的是,根据Bash参考手册:

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

任何简单的命令或函数的环境都可以通过预先设置参数赋值来临时增强,如Shell参数中所描述的那样。这些赋值语句只影响该命令所看到的环境。


To sum things up:

总结:

  • export is used to set environment variable in operating system. This variable will be available to all child processes created by current Bash process ever after.
  • 导出用于在操作系统中设置环境变量。此变量将可用于当前Bash进程创建的所有子进程。
  • Bash variable notation (name=value) is used to set local variables available only to current process of bash
  • Bash变量表示法(name=value)用于设置仅用于当前Bash进程的局部变量。
  • Bash variable notation prefixing another command creates environment variable only for scope of that command.
  • Bash变量表示法前缀另一个命令仅为该命令的范围创建环境变量。

#9


3  

Here's yet another example:

这是另一个例子:

VARTEST="value of VARTEST" 
#export VARTEST="value of VARTEST" 
sudo env | grep -i vartest 
sudo echo ${SUDO_USER} ${SUDO_UID}:${SUDO_GID} "${VARTEST}" 
sudo bash -c 'echo ${SUDO_USER} ${SUDO_UID}:${SUDO_GID} "${VARTEST}"'  

Only by using export VARTEST the value of VARTEST is available in sudo bash -c '...'!

只有通过使用export VARTEST,在sudo bash -c '…'中才能使用VARTEST的值。

For further examples see:

为进一步的例子:

  • http://mywiki.wooledge.org/SubShell

    http://mywiki.wooledge.org/SubShell

  • bash-hackers.org/wiki/doku.php/scripting/processtree

    bash-hackers.org/wiki/doku.php/scripting/processtree

#10


3  

The accepted answer implies this, but I'd like to make explicit the connection to shell builtins:

这个被接受的答案暗示了这一点,但我想把它与shell builtins联系起来:

As mentioned already, export will make a variable available to both the shell and children. If export is not used, the variable will only be available in the shell, and only shell builtins can access it.

正如前面提到的,导出将使shell和children都可以使用一个变量。如果不使用导出,则该变量只能在shell中可用,只有shell builtins可以访问它。

That is,

也就是说,

tango=3
env | grep tango # prints nothing, since env is a child process
set | grep tango # prints tango=3 - "type set" shows `set` is a shell builtin

#11


2  

Just to show the difference between an exported variable being in the environment (env) and a non-exported variable not being in the environment:

为了说明在环境中导出的变量(env)和不存在于环境中的非导出变量之间的区别:

If I do this:

如果我这样做:

$ MYNAME=Fred
$ export OURNAME=Jim

then only $OURNAME appears in the env. The variable $MYNAME is not in the env.

然后只有$OURNAME出现在env中。变量$MYNAME不在env中。

$ env | grep NAME
OURNAME=Jim

but the variable $MYNAME does exist in the shell

但是变量$MYNAME确实存在于shell中。

$ echo $MYNAME
Fred

#12


2  

Two of the creators of UNIX, Brian Kernighan and Rob Pike, explain this in their book "The UNIX Programming Environment". Google for the title and you'll easily find a pdf version.

UNIX的两个创建者Brian Kernighan和Rob Pike在他们的书“UNIX编程环境”中解释了这一点。标题的谷歌,你很容易找到pdf版本。

They address shell variables in section 3.6, and focus on the use of the export command at the end of that section:

它们在第3.6节中处理shell变量,并将重点放在该部分末尾的export命令的使用:

When you want to make the value of a variable accessible in sub-shells, the shell's export command should be used. (You might think about why there is no way to export the value of a variable from a sub-shell to its parent).

当您想要在子shell中获取变量的值时,应该使用shell的导出命令。(您可能会想,为什么没有方法将变量的值从子shell导出到其父节点)。

#13


1  

Although not explicitly mentioned in the discussion, it is NOT necessary to use export when spawning a subshell from inside bash since all the variables are copied into the child process.

虽然在讨论中没有明确提到,但是当在bash中生成子shell时,并不需要使用导出,因为所有的变量都被复制到子进程中。

#14


0  

By default, variables created within a script are only available to the current shell; child processes (sub-shells) will not have access to values that have been set or modified. Allowing child processes to see the values, requires use of the export command.

默认情况下,在脚本中创建的变量只对当前shell可用;子进程(子shell)将无法访问已设置或修改的值。允许子进程查看值,需要使用导出命令。


推荐阅读
author-avatar
爱圣翼迹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有