shell变量赋值与环境
今天,跟着架构师(凡爷)搞了一个代码量统计的脚本,我学到了以下几条:
1)命令在脚本里,只是字符串,较长的命令可以改为变量,引用去执行
2)潜意识我想到if,添加语句块,而凡爷却想到了grep -v,节省了一大段代码
3)echo '' > stat 他自然而然地加上这一行,让output按不同区块相隔开,结构清晰
4)他定位问题精准而快速,很是钦佩
今晚关于shell变量,我做了很多测试,将其整理如下,以便温故而知新。
#赋值#
# hours_per_day=24 seconds_per_hour=3600 days_per_week=7
# echo $hours_per_day $seconds_per_hour $days_per_week
24 3600 7
readonly 使变量成为只读模式,而对它们赋值是被禁止的# readonly hours_per_day seconds_per_hour days_per_week # hours_per_day=2-bash: hours_per_day: readonly variable# echo $hours_per_day $seconds_per_hour $days_per_week24 3600 7
export [-fn] [name[=word]] ... 用于创建(空也可以)、修改、删除、打印“环境”(environment)
export -p
The supplied names are marked for automatic export to the environment of subsequently executed commands.
“提供的names被标记为自动导出给后续执行命令的环境。”
If the -f option is given, the names refer to functions.
如果指定-f,names引用函数。(即代表函数名称,函数名也是变量名)
If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed.
如果没有names给出或者提供-p选项,所有names的一个list在这个shell里导出(列出)
The -n option causes the export property to be removed from each name.
如果是-n选项,导致属性从每一个name中删除。(事实上并未删除,只是不会输出到后续指令的执行环境中,在当前shell中还是有)
If a variable name is followed by =word, the value of the variable is set to word. export returns an exit status of 0 unless an invalid option is encountered,
one of the names is not a valid shell variable name, or -f is supplied with a name that is not a func‐tion.
如果变量name后跟=word,则变量的值设置为word,export返回状态码为0,除非遇到一个无效选项、其中一个name不是有效的shell变量名,或者-f后面提供不是name不是function。
export是将变量放进环境(environment)里,环境是一个name与word的简单列表,可供所有执行中的程序使用。
新的进程会从其父进程集成环境,也可以在建立新的子进程之前修改它。
如
PATH=$PATH:/usr/local/bin 更新PATH export PATH 导出它(发布它) $export -p 显示当前环境
env [-i] [ var=value ...] [ command_name [ arguments... ] ]
-i 忽略继承的环境,仅使用命令行上所给出的变量与值。
未提供command_name时,显示环境中所有变量的name与value,否则使用命令行中提供的变量复制。
如:
$ cat test.sh
echo $HOSTNAME
env
$$ env -i HOSTNAME=30 PATH=/bin:/usr/bin/ name=abc sh test.sh (忽略继承环境,只使用命令行提供的变量与值) 30
HOSTNAME=30
name=abc
PATH=/bin:/usr/bin/
PWD=/home/zhangchunyang
SHLVL=1
_=/bin/env
$ env HOSTNAME=30 PATH=/bin:/usr/bin/ sh test.sh | less (继承环境,同名的则覆盖) 30
HOSTNAME=30
PYENV_ROOT=/home/zhangchunyang/.pyenv
SHELL=/bin/bash
TERM=xterm
HISTSIZE=5
CATALINA_HOME=/usr/local/tomcat
SSH_CLIENT=117.131.199.244 31299 22
SYBASE_JRE7=/home/opt/sybase/shared/JRE-7_0_21_64BIT
LUAJIT_LIB=/usr/local/lib
PYTHON_EGG_CACHE=/tmp/.python-eggs
注意:打印时,env不会正确地为环境变量值加上引号,以供重新输入到shell中,如果需要此功能,可使用export -p
unset [-v] variable ...
unset -f function ...
从当前shell删除变量与函数 可以是任何变量(局部变量、环境变量)
-f: 解除(删除)指定的函数
-v: 解除(删除)指定的变量。 没有任何选项时,这是默认行为模式
如: unset full_name 删除full_name unset -v first middle last 删除这些变量
who_is_on () {
who | awk '{print $1}' | sort -u
}
...
unset -f who 删除函数
$ cat test.sh
a=1
b=2
c=3
d=4
e=5
f=6
who_is_on () {
who | awk '{print $1}' | sort -u
}
unset -v b
unset -f who_is_on
echo $a $b $c $d $e $f
who_is_on
$ sh test.sh
1 3 4 5 6
test.sh: line 14: who_is_on: command not found
$ unset PATH
$ ls
-bash: ls: No such file or directory
$ vim
-bash: vim: No such file or directory
declare [-aAfFgilrtux] [-p] [name[=value] ...] #查看变量属性
typeset [-aAfFgilrtux] [-p] [name[=value] ...] 声明/宣告 变量
-a Each name is an indexed array variable (see Arrays above) 声明name是索引数组变量
-A Each name is an associative array variable (see Arrays above). 声明name是关联数组变量
-f Use function names only. 使用函数名
-i The variable is treated as an integer; arithmetic evaluation (see ARITHMETIC EVALUATION above) is
performed when the variable is assigned a value. 声明整型变量。当变量分配值时被表现为算术求值。
-l When the variable is assigned a value, all upper-case characters are converted to lower-case.
The upper-case attribute is disabled. 禁用大写,分配的大写自动转为小写
-r Make names readonly. These names cannot then be assigned values by subsequent assignment state‐
ments or unset. 设置为只读变量。这些names不能被后续再分配状态(修改)和删除(unset)
-t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the
calling shell. The trace attribute has no special meaning for variables. 给name trace属性
-u When the variable is assigned a value, all lower-case characters are converted to upper-case.
The lower-case attribute is disabled. 禁用小写,分配的值为小写自动转为大写
-x Mark names for export to subsequent commands via the environment. 标记names为export给后续命令的环境
-x 即export 发布的变量
# export -p | less
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T:%t"
declare -x HOME="/root"
declare -x HOSTNAME="Webserver"
...
$ declare -u f
$ f='sdafsadf'
$ declare -u
declare -u f="SDAFSADF"
上面这些属性是可以叠加的
如declare -irx UID="0"参数展开(parameter expansion)是shell提供变量值在程序中使用的过程。