Shell Variables and Environment Variables
每个unix 进程都运行在一个特定的环境(environment)中,environment是由一个包含了environment varialbes 的table组成每隔variable都有被指定好的值。
当你登录(log in)的时候,一些特定的login files会被执行。(注:我觉得这里是在说.profile .bashrc这类东西)
他们会初始化table,用来维持进程的environment variables。
当一个父进程开启一个子进程的时候,子进程会被给一份父进程table的拷贝。
环境变量(Environment variable)一般都是用大写表示的。
shell也维持了一些内部的变量,被称作 shell variables,这些变量让shell按某种方式工作。shell variables只在他们被定义的shell内部有效。他们不会对父shell或者子shell有效,通常,shell变量使用小写表示的(C shell family),用大写表示(Bourne shell family).
C Shell Family
C shell family明确的区分了 shell variables和environmentsvariables
Shell Variables
一个shell variable要用set命令来定义,并用unset命令来删除,.cshrc文件(本文的最后会讨论)的主要目的就是为每隔进程定义这些变量。为了定义一个新的变量或者修改已经定义的变量的值,输入:
set name = value
name是指变量的名称,value是变量的值,如果value是一个list,在他们的外面使用括号来定义变量
set name = (value1 value2 value3)
单独写set命令而不传参数,会显示你所有的shell variables,你不能单独的去查看某一个变量的值,比如使用 set name,如果不传=value的话,会最终给这个变量赋一个空值。
为了删除,unset一个shell变量,你需要:
unset name
为了使用shell 变量,你需要在他前面加一个$,比如说$name,这个会告诉command interpreter 你想要的是变量的值,而不是他的名字。 你也可以使用 ${name},这样可以避免连接text时候的confusion( which avoids confusion when concatenated with text )
为了查看一个变量的名字,使用 echo命令:
echo $name
如果变量是一个list,为了看到list中第n个项的值L
echo $name[n]
必须要传入方括号,并且名字和方括号之间不能有空格。
为了prepend或者append一个值到已有的shell 变量中,使用下面的语法:
set name = prepend_value${name}
或者
set name = ${name}append_value
记住当一个shell启动的时候,有四个重要的shell variables会自动初始化,并携带着相应的environment variable的值,是: user, term, home, path如果这些有改变,相应的环境变量(environment variable)也会改变。
Environment Variables
环境变量是通过setenv命令来设置的,并且通过printenv或者env命令来展示,或者通过echo命令(展示单独一个变量的时候),有些环境变量(environment variables)会被默认设置好。
这些命令的格式是(注意set和setenv的区别):
setenv [NAME value]
unsetenv NAME
如果想包含多个值,中间要加上空格,使用双引号包住它
setenv NAME "value1 value2 ..."
当前的环境变量设置可以通过setenv命令查看(不传参数)
为了使用环境变量,使用$前缀,你还可以使用${NAME}。
为了prepend或者append一个值到当前的环境变量
使用
setenv NAME "prepend_value${NAME}"
或者
setenv NAME "${NAME}append_value"
如果pre或者append的值是当前已经存在的一个环境变量,把变量的名字也用花括号括住:
setenv NAME "${NAME}${XYZ_VAR}"
append和prepending通常和PAT变量一起使用,并且使用colon(冒号)作为分隔符:
setenv PATH "${PATH}:${XYZ_DIR}"
Bourne Shell Family
Bourne shell family不是很区分shell变量和环境变量,当一个shell启动的时候,它会去读取environment variables table中的信息,为每个给他自己定义一个shell variable,使用相同的名字(而且还用大写),并且复制值。
从这一点来看,shell只包含它自己的shell variables。
如果对shell variable作了修改,必须显式的“export"到当前的环境变量中,为了让当前的forked的子进程来看到变更。
shell变量的赋值:
NAME=value; export NAME
注意=号中间没有空格。大多数情况下,你都想把它export出去
unset NAME
NAME = "value1 value2 ..."
当前所有的变量可以通过set命令来查看,
echo $NAME
echo
{$NAME}
NAME=prepend_value$NAME
NAME=$NAMEappend_value
append和prepend通常用在PATH变量中:
PATH=${PATH}:${XYZ_DIR}
注:csh已经很少用了,现在都用bash。
资料:https://web.archive.org/web/20170713001430/http://sc.tamu.edu/help/general/unix/vars.html}
资料二:
https://unix.stackexchange.com/questions/368944/what-is-the-difference-between-env-setenv-export-and-when-to-use
export VARIABLE_NAME=‘some value‘ is the way to set an environment variable in any POSIX-compliant shell (sh, dash, bash, ksh, etc.; also zsh). If the variable already has a value, you can use export VARIABLE_NAME to make it an environment variable without changing its value.
Pre-POSIX Bourne shells did not support this, which is why you‘ll see scripts that avoid export VARIABLE_NAME=‘some value‘ and use VARIABLE_NAME=‘some value‘; export VARIABLE_NAME instead. But pre-POSIX Bourne shells are extremely rare nowadays.
setenv VARIABLE_NAME=‘some value‘ is the csh syntax to set an environment variable. setenv does not exist in sh, and csh is extremely rarely used in scripts and has been surpassed by bash for interactive use for the last 20 years (and zsh for even longer), so you can forget about it unless you encounter it.
The env command is very rarely useful except in shebang lines. When invoked without arguments, it displays the environment, but export does it better (sorted, and often quoted to disambiguate newlines in values from newlines that separate values). When invoked with arguments, it runs a command with extra environment variables, but the same command without env also works (VAR=value mycommand runs mycommand with VAR set to value, just like env VAR=value mycommand). The reason env is useful in shebang line is that it performs PATH lookup, and it happens to not do anything else when invoked with a command name. The env command can be useful to run a command with only a few environment variables with -i, or without parameters to display the environment including variables with invalid names that the shell doesn‘t import.
资料三:
https://askubuntu.com/questions/205688/whats-the-difference-between-set-export-and-env-and-when-should-i-use-each
(export 命令 不带参数,其实就相当于env,看环境变量,但是这个是bash独有的,而且,相比env输出的结果是有排序的)
资料4:
https://unix.stackexchange.com/questions/8592/whats-the-difference-between-export-and-setenv
Shell variables Environment variables export set env
原文:https://www.cnblogs.com/eret9616/p/14687652.html