shell备忘录

189 阅读1分钟

变量

环境变量 说明
$HOME 当前用户的home目录
$PATH 以冒号分割的用来搜索命令的目录列表
$PS1 命令提示符,例如:[\u@\h \w]$,给出用户名 机器名和当前目录名 后面跟一个$提示符号
$PS2 二级提示符,用来提示后续的输入,通常是>字符
$IFS 输入域分割符。当shell读取输入时,它给出用来分隔单词的一组字符,它们通常是空格 制表符和换行符
$0 shell脚步的名字
$# 传递给脚本的参数个数
? 执行当前shell脚本的进程号
参数变量 说明
$1,$2, ... 脚本程序的参数
$* 在一个变量中列出所有的参数,各个参数之间用环境变量IFS中的第一个字符分隔开
$@ 它是$*的一种精巧变体,使用空格分隔各个参数

当函数被调用时,脚本程序的位置参数($* $@ $# $1 $2等)会被替换为函数的参数

条件比较

字符串比较 说明
string1 = string2 如果量个字符串相同则结果为真
string1 != string2 如果量个字符串不同则结果为真
-n string 如果字符串不为空则结果为真
-z string 如果字符串为空则结果为真
算数比较 说明
expression1 -eq expression2 True if the expressions are equal
expression1 -ne expression2 True if the expressions are not equal
expression1 -gt expression2 True if expression1 is greater than expression2
expression1 -ge expression2 True if expression1 is greater than or equal to expression2
expression1 -lt expression2 True if expression1 is less than expression2
expression1 -le expression2 True if expression1 is less than or equal to expression2
文件条件测试 说明
-d file True if the file is a directory
-e file True if the file exists. Note that historically the -e option has not been portable, so -f is usually used.
-f file True if the file is a regular file
-g file True if set-group-id is set on file
-r file True if the file is readable
-s file True if the file has nonzero size
-u file True if set-user-id is set on file
-w file True if the file is writable
-x file True if the file is executable