变量
变量
Shell 中存在三种变量:
- 局部变量:局部变量在脚本或命令中定义,仅在当前 Shell 实例中有效,其他 Shell 启动的程序不能访问局部变量。
- 环境变量:所有的程序,包括 Shell 启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候 Shell 脚本也可以定义环境变量。
- Shell 变量:Shell 变量是由 Shell 程序设置的特殊变量。Shell 变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了 Shell 的正常运行。
Shell 变量
System Variable | Meaning | To View Variable Value Type |
---|---|---|
BASH_VERSION | Holds the version of this instance of bash. | echo $BASH_VERSION |
HOSTNAME | The name of the your computer. | echo $HOSTNAME |
CDPATH | The search path for the cd command. | echo $CDPATH |
HISTFILE | The name of the file in which command history is saved. | echo $HISTFILE |
HISTFILESIZE | The maximum number of lines contained in the history file. | echo $HISTFILESIZE |
HISTSIZE | The number of commands to remember in the command history. The default value is 500. | echo $HISTSIZE |
HOME | The home directory of the current user. | echo $HOME |
IFS | The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is |
echo $IFS |
LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_ . |
echo $LANG |
PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. | echo $PATH |
PS1 | Your prompt settings. | echo $PS1 |
TMOUT | The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. | echo $TMOUT |
TERM | Your login terminal type. | echo $TERM export TERM=vt100 |
SHELL | Set path to login shell. | echo $SHELL |
DISPLAY | Set X display name | echo $DISPLAY export DISPLAY=:0.1 |
EDITOR | Set name of default text editor. | export EDITOR=/usr/bin/vim |
变量定义
定义变量时,变量名不加美元符号,并且变量名和等号之间不能有空格;使用一个定义过的变量,只要在变量名前面加美元符号即可,如:
your_name="qinjx"
echo $your_name
echo ${your_name}
在程序执行过程中,使用 unset 命令删除变量。它可以删除函数和 Shell 变量。
# 删除变量
unset variable_name
vech=Bus
echo $vech
unset vech
echo $vech
注意,变量赋值的时候等号两边不可以包含空格:
no =10
no= 10
no = 10
我们也可以将某个语句的执行结果赋值给某个变量:
for file in $(ls /etc)
for file in `ls /etc`
变量名外面的花括号是可选的,为了帮助解释器识别变量的边界,区分譬如 ${skill}Script
这样的情况。除了显式地直接赋值,还可以用语句给变量赋值,如:
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
只读变量
使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变:
myUrl="http://www.google.com"
readonly myUrl
创建一个名为 DATA 的常量变量,并使其值在整个 Shell 脚本中始终相同,即不能更改:
readonly DATA=/home/sales/data/feb09.dat
echo $DATA
/home/sales/data/feb09.dat
DATA=/tmp/foo
# Error ... readonly variable
并且 unset 关键字不可作用于只读变量。
变量导出
我们可以使用 export 来导出变量:
export backup="/nas10/mysql"
echo "Backup dir $backup"
bash
echo "Backup dir $backup"
export -p
默认情况下,所有用户定义的变量都是局部变量。它们不会导出到新流程。使用 export 命令将变量和函数导出到子进程。如果没有给出变量名或函数名,或者如果给出了-p 选项,则将打印出在此 shell 中导出的所有名称的列表。-n 的参数表示要从后续的 NAME 中删除导出属性。
变量检测与默认值
存在性检测
如果未定义变量,则可以使用以下语法停止执行 Bash 脚本:
${varName?Error varName is not defined}
${varName:?Error varName is not defined or is empty}
创建一个名为 varcheck.sh 的 shell 脚本:
#!/bin/bash
# varcheck.sh: Variable sanity check with :?
path=${1:?Error command line argument not passed}
echo "Backup path is $path."
echo "I'm done if $path is set."
Run it as follows:
chmod +x varcheck.sh
./varcheck.sh /home
Backup path is /home.
I'm done if $path is set.
Run the script without any arguments:
./varcheck.sh: line 3: 1: Error command line argument not passed
变量默认值
我们可以使用如下方式设置变量的默认值:
$ echo ${grandslam=Maria Sharapova}
$ echo ${grandslam:-DefaultValueHere}
$ msg=${1:-update articles or snippets}
# 返回默认值且赋值
$ echo ${grandslam:=Maria Sharapova}
很多时候在函数调用的时候,也可以通过这种方式传入默认值:
die(){
local error=${1:=Undefined error}
echo "$0: $LINE $error"
}
die "File not found"
die