使用 Bash 在 Linux 中设置环境变量

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

Setting environment variables in Linux using Bash

linuxbashunixshell

提问by pbh101

In tcsh, I have the following script working:

tcsh,我有以下脚本工作:

#!/bin/tcsh
setenv X_ROOT /some/specified/path

setenv XDB    ${X_ROOT}/db
setenv PATH   ${X_ROOT}/bin:${PATH}

xrun -d xdb1 -i  > 

What is the equivalent to the tcsh setenvfunction in Bash?

tcsh setenv与 Bash 中的函数等价的是什么?

Is there a direct analog? The environment variables are for locating the executable.

有直接模拟吗?环境变量用于定位可执行文件。

采纳答案by mipadi

export VAR=valuewill set VAR to value. Enclose it in single quotes if you want spaces, like export VAR='my val'. If you want the variable to be interpolated, use double quotes, like export VAR="$MY_OTHER_VAR".

export VAR=value将 VAR 设置为值。如果需要空格,请将其括在单引号中,例如export VAR='my val'. 如果您希望对变量进行插值,请使用双引号,例如export VAR="$MY_OTHER_VAR".

回答by Oli

I think you're looking for export- though I could be wrong.. I've never played with tcsh before. Use the following syntax:

我认为您正在寻找export-尽管我可能是错的..我以前从未玩过 tcsh。使用以下语法:

export VARIABLE=value

回答by iny

VAR=valuesets VAR to value.

VAR=value将 VAR 设置为值。

After that export VARwill give it to child processes too.

之后export VAR也会将其提供给子进程。

export VAR=valueis a shorthand doing both.

export VAR=value是两者兼而有之的速记。

回答by zaphod

The reason people often suggest writing

人们经常建议写作的原因

VAR=value
export VAR

instead of the shorter

而不是较短的

export VAR=value

is that the longer form works in more different shells than the short form. If you know you're dealing with bash, either works fine, of course.

是较长的形式比短形式在更多不同的壳中起作用。如果您知道您正在处理bash,那么当然可以正常工作。

回答by Eric Leschinski

Set a local and environment variable using Bash on Linux

在 Linux 上使用 Bash 设置本地和环境变量

Check for a local or environment variables for a variable called LOL in Bash:

检查 Bash 中名为 LOL 的变量的本地或环境变量:

el@server /home/el $ set | grep LOL
el@server /home/el $
el@server /home/el $ env | grep LOL
el@server /home/el $

Sanity check, no local or environment variable called LOL.

健全性检查,没有称为 LOL 的本地或环境变量。

Set a local variable called LOL in local, but not environment. So set it:

在本地而不是环境中设置一个名为 LOL 的局部变量。所以设置它:

el@server /home/el $ LOL="so wow much code"
el@server /home/el $ set | grep LOL
LOL='so wow much code'
el@server /home/el $ env | grep LOL
el@server /home/el $

Variable 'LOL' exists in local variables, but not environment variables. LOL will disappear if you restart the terminal, logout/login or run exec bash.

变量“LOL”存在于局部变量中,但不存在于环境变量中。如果您重新启动终端,注销/登录或运行 LOL 将消失exec bash

Set a local variable, and then clear out all local variables in Bash

设置一个局部变量,然后在Bash中清除所有局部变量

el@server /home/el $ LOL="so wow much code"
el@server /home/el $ set | grep LOL
LOL='so wow much code'
el@server /home/el $ exec bash
el@server /home/el $ set | grep LOL
el@server /home/el $

You could also just unset the one variable:

您也可以取消设置 one 变量:

el@server /home/el $ LOL="so wow much code"
el@server /home/el $ set | grep LOL
LOL='so wow much code'
el@server /home/el $ unset LOL
el@server /home/el $ set | grep LOL
el@server /home/el $

Local variable LOL is gone.

局部变量 LOL 不见了。

Promote a local variable to an environment variable:

将局部变量提升为环境变量:

el@server /home/el $ DOGE="such variable"
el@server /home/el $ export DOGE
el@server /home/el $ set | grep DOGE
DOGE='such variable'
el@server /home/el $ env | grep DOGE
DOGE=such variable

Note that exporting makes it show up as both a local variable and an environment variable.

请注意,导出使其同时显示为局部变量和环境变量。

Exported variable DOGE above survives a Bash reset:

上面导出的变量 DOGE 在 Bash 重置后仍然存在:

el@server /home/el $ exec bash
el@server /home/el $ env | grep DOGE
DOGE=such variable
el@server /home/el $ set | grep DOGE
DOGE='such variable'

Unset all environment variables:

取消设置所有环境变量:

You have to pull out a can of Chuck Norris to reset all environment variables without a logout/login:

您必须拿出一罐 Chuck Norris 来重置所有环境变量,而无需注销/登录:

el@server /home/el $ export CAN="chuck norris"
el@server /home/el $ env | grep CAN
CAN=chuck norris
el@server /home/el $ set | grep CAN
CAN='chuck norris'
el@server /home/el $ env -i bash
el@server /home/el $ set | grep CAN
el@server /home/el $ env | grep CAN

You created an environment variable, and then reset the terminal to get rid of them.

您创建了一个环境变量,然后重置终端以摆脱它们。

Or you could set and unset an environment variable manually like this:

或者您可以像这样手动设置和取消设置环境变量:

el@server /home/el $ export FOO="bar"
el@server /home/el $ env | grep FOO
FOO=bar
el@server /home/el $ unset FOO
el@server /home/el $ env | grep FOO
el@server /home/el $