setenv VAR [ VALUE ]
争论
VAR
|
要设置的变量的名称。
|
VALUE
|
变量的值,可以是单个单词,也可以是带引号的字符串。
|
描述
setenv是C shell(csh)的内置函数。它用于定义环境变量的值。
如果没有给setenv参数,它将显示所有环境变量及其值。如果仅指定VAR,则它将该名称的环境变量设置为空(null)值。如果两个VAR和VALUE是指定的,它集命名变量VAR的值VALUE。setenv类似于set命令,该命令还设置环境变量的值。但是,与set不同,setenv还将此环境变量“导出”到任何子shell。这样,它等效于bash命令出口。
例如,如果您在c shell中,则使用setenv设置以下变量:
setenv MYVAR myvalue
然后,我们可以使用echo命令查看该变量的值:
echo"$MYVAR"
myvalue
我们的值“ myvalue ”被返回。现在让我们将bash作为子shell运行:
bash
并查看它是否知道变量MYVAR的值:
echo"$MYVAR"
myvalue
如您所见,MYVAR的值传递给bash。
现在,让我们看看set有何不同。让我们退出bash子外壳,回到csh:
exit
...并使用set设置另一个环境变量MYVAR2:
set MYVAR2 = myvalue2
(如您所见,set的语法略有不同。它使用等号分配值。)现在,让我们检查MYVAR2的值:
echo"$MYVAR2"
myvalue2
现在让我们回到bash:
bash
...并检查MYVAR2的值:
echo"$MYVAR2"
这次,没有报告任何值,因为该变量未“导出”到子shell。因此,当您使用csh时,如果您希望环境变量仅在当前外壳程序中保持局部状态,请使用set。如果您还希望它们继承到子shell,请使用setenv。
setenv VAR [VALUE]
Arguments
VAR
|
The name of the variable to be set.
|
VALUE
|
The value of the variable, as either a single word or a quoted string.
|
Description
setenv is a built-in function of the C shell (csh). It is used to define the value of environment variables.
If setenv is given no arguments, it displays all environment variables and their values. If only VAR is specified, it sets an environment variable of that name to an empty (null) value. If both VAR and VALUE are specified, it sets the variable named VAR to the value VALUE. setenv is similar to the set command, which also sets an environment variable's value. However, unlike set, setenv also "exports" this environment variable to any subshells. In this way, it is the equivalent of the bash command export.
For instance, if you are inside the c shell, and you use setenv to set the following variable:
setenv MYVAR myvalue
We can then use the echo command to view the value of that variable:
echo "$MYVAR"
myvalue
Our value, "myvalue", was returned. Now let's run bash as a subshell:
bash
and see if it knows the value of our variable MYVAR:
echo "$MYVAR"
myvalue
As you can see, the value of MYVAR was passed on to bash.
Now, let's see how set is different. Let's go back to csh by exiting the bash subshell:
exit
...and use set to set another environment variable, MYVAR2:
set MYVAR2=myvalue2
(The syntax of set, as you can see, is slightly different. It uses an equals sign to assign a value.) Now let's check the value of MYVAR2:
echo "$MYVAR2"
myvalue2
And now let's go back to bash:
bash
...and check the value of MYVAR2:
echo "$MYVAR2"
This time, no value is reported, because the variable was not "exported" to the subshell. So, when you are using csh, if you want environment variables to remain local to only the current shell, use set. If you want them to carry over to subshells as well, use setenv.