setenv (添加或更改环境变量的值)

瑞兹 发表于 2020-11-30 09:03
浏览次数:
在手机上阅读

在运行C shell类的Unix操作系统上,setenv内置命令会添加或更改环境变量的值。

查看英文版

目录

1 setenv 运行系统环境

2 setenv 语法

3 setenv 例子

setenv 运行系统环境

Unix&Linux

setenv 语法

setenv VAR [ VALUE ]

争论

VAR 要设置的变量的名称。
VALUE 变量的值,可以是单个单词,也可以是带引号的字符串。

描述

setenv是C shell(csh)的内置函数。它用于定义环境变量的值。

如果没有给setenv参数,它将显示所有环境变量及其值。如果仅指定VAR,则它将该名称的环境变量设置为空(null)值。如果两个VARVALUE是指定的,它集命名变量VAR的值VALUEsetenv类似于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 VALUEsetenv is similar to the set command, which also sets an environment variable's value. However, unlike setsetenv 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.

查看英文版

查看中文版

setenv 例子

setenv PATH "/bin:/usr/bin:/usr/sbin:/usr/local/bin"

设置环境变量PATH。PATH是用冒号(“:”)分隔的路径名列表,这些是调用命令时搜索可执行文件的默认路径。将PATH设置为上述值后,shell将按顺序在路径/bin、/usr/bin、/usr/sbin/usr/local/bin中查找您运行的任何后续命令的可执行文件。

setenv PATH "/bin:/usr/bin:/usr/sbin:/usr/local/bin"

Sets the environment variable PATH. PATH is a list of path names separated by colons (":"), which are the default paths to search for executable files when a command is called. After you set PATH to the above value, the shell will look in the paths /bin/usr/bin/usr/sbin, and /usr/local/bin, in that order, for the executable files of any subsequent commands that you run.

查看英文版

查看中文版

其他命令行

sysklogd | s2p | service | sag | set | setfacl | sfdisk | sha224sum | sha256sum | sha384sum | sha512sum | shutdown | sleep | stat | startx | strftime | strip | swapoff | shred |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条