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

时间:2021-09-07 11:03:12

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 $1 > $2

What is the equivalent to the tcsh setenv function in Bash?

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

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

有直接的类比吗?环境变量用于定位可执行文件。

5 个解决方案

#1


92  

export VAR=value will 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".

导出VAR=value将VAR设置为value。如果需要空格,可以用单引号括起来,比如export VAR='my val'。如果想要插入变量,请使用双引号,比如export VAR=“$MY_OTHER_VAR”。

#2


30  

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,当然,这两种方法都可以。

#3


21  

Set a local and environment variable using Bash on Linux

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'存在于局部变量中,而不是环境变量。如果重新启动终端、登出/登录或运行exec bash, LOL将消失。

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:

你也可以不设置一个变量:

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 $

#4


11  

VAR=value sets VAR to value.

值设置VAR值。

After that export VAR will give it to child processes too.

在导出VAR之后,它也将被赋予子进程。

export VAR=value is a shorthand doing both.

export VAR=value是两者都做的简写。

#5


6  

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

我想你是在寻找出口——尽管我可能是错的。我从来没有和tcsh玩过。使用下面的语法:

export VARIABLE=value

#1


92  

export VAR=value will 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".

导出VAR=value将VAR设置为value。如果需要空格,可以用单引号括起来,比如export VAR='my val'。如果想要插入变量,请使用双引号,比如export VAR=“$MY_OTHER_VAR”。

#2


30  

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,当然,这两种方法都可以。

#3


21  

Set a local and environment variable using Bash on Linux

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'存在于局部变量中,而不是环境变量。如果重新启动终端、登出/登录或运行exec bash, LOL将消失。

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:

你也可以不设置一个变量:

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 $

#4


11  

VAR=value sets VAR to value.

值设置VAR值。

After that export VAR will give it to child processes too.

在导出VAR之后,它也将被赋予子进程。

export VAR=value is a shorthand doing both.

export VAR=value是两者都做的简写。

#5


6  

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

我想你是在寻找出口——尽管我可能是错的。我从来没有和tcsh玩过。使用下面的语法:

export VARIABLE=value