1. 函数
先定义后使用
函数定义
foo() {
}
function foo {
}
函数调用
foo
参数传递
var="Hello world"
foo ${var}
参数传递的是字符串,在 foo 函数中,$1="Hello" $2="world"
2. bash 参数
$0 脚本名
$1 第一个参数
$2 第二个参数
[email protected] 或 $* 所有参数
$# 参数个数
shift 向左移动参数,默认是 shift 1
3. 字符串替换
${VAR/a/A}
Substitute the first ‘a‘ with ‘A‘
${VAR//a/A}
Substitute all ‘a‘ with ‘A‘
${VAR//[ |:]/-}
Substitute all ‘ ‘ or ‘:‘ with ‘-‘
4. if 用法
1. use []
2. after ‘[‘ and before ‘]‘ have a space
if [ "$VAL"x = ""x ];then
fi
5. for 用法
for ((i=0; i<10; ++i)); do
echo $i
done
6. Get current shell directory
SHELL_FOLDER=$(dirname $(readlink -f "$0"))
7 ‘‘ "" `` 的区别
‘‘
Keep the char original.
""
Translate the special char.
``
Execute the command.
8. 命令返回值
$? stores the return code.