bash UNIX 导出命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7328223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:01:06  来源:igfitidea点击:

UNIX export command

bashshellunix

提问by Jake

I am trying to understand the use of exportcommand.

我试图了解export命令的使用。

I tried using man export, but there is no manual for this command.

我尝试使用man export,但没有此命令的手册。

Can anyone please help me out understanding the use of exportin UNIX?

任何人都可以帮助我理解export在 UNIX 中的使用吗?

回答by John Kugelman

When you execute a program the child program inherits its environment variables from the parent. For instance if $HOMEis set to /rootin the parent then the child's $HOMEvariable is also set to /root.

当您执行程序时,子程序从父程序继承其环境变量。例如,如果在父项中$HOME设置为/root,那么子项的$HOME变量也设置为/root

This only applies to environment variable that are marked for export. If you set a variable at the command-line like

这仅适用于标记为导出的环境变量。如果您在命令行中设置一个变量,例如

$ FOO="bar"

That variable will not be visible in child processes. Not unless you export it:

该变量在子进程中不可见。除非你导出它:

$ export FOO

You can combine these two statements into a single one in bash (but not in old-school sh):

您可以在 bash 中将这两个语句合并为一个(但不能在老式 sh 中):

$ export FOO="bar"

Here's a quick example showing the difference between exported and non-exported variables. To understand what's happening know that sh -ccreates a child shell process which inherits the parent shell's environment.

这是一个快速示例,显示了导出变量和非导出变量之间的区别。要了解正在发生的事情,请知道sh -c创建一个继承父 shell 环境的子 shell 进程。

$ FOO=bar
$ sh -c 'echo $FOO'

$ export FOO
$ sh -c 'echo $FOO'
bar

Note:To get help on shell built-in commands use help export. Shell built-ins are commands that are part of your shell rather than independent executables like /bin/ls.

注意:要获得有关 shell 内置命令的帮助,请使用help export. Shell 内置命令是作为 Shell 一部分的命令,而不是像/bin/ls.

回答by user931841

Unix

Unix

The commands env, set, and printenv display all environment variables and their values. env and set are also used to set environment variables and are often incorporated directly into the shell. printenv can also be used to print a single variable by giving that variable name as the sole argument to the command.

命令 env、set 和 printenv 显示所有环境变量及其值。env 和 set 也用于设置环境变量,通常直接合并到 shell 中。通过将变量名称作为命令的唯一参数,printenv 还可用于打印单个变量。

In Unix, the following commands can also be used, but are often dependent on a certain shell.

在 Unix 中,也可以使用以下命令,但通常依赖于某个 shell。

export VARIABLE=value  # for Bourne, bash, and related shells
setenv VARIABLE value  # for csh and related shells

You can have a look at thisat

你可以看看这个

回答by David J. Liszewski

exportis a built-in command of the bashshelland other Bourne shell variants. It is used to mark a shell variable for export to child processes.

exportbashshell和其他Bourne shell 变体的内置命令。它用于标记要导出到子进程的 shell 变量。

回答by Icarus

exportis used to set environment variables. For example:

export用于设置环境变量。例如:

export EDITOR=pico

Will set your default text editor to be the picocommand.

将您的默认文本编辑器设置为pico命令。