符号 $
var
$str表示变量,可赋值等操作,下面是一些特殊的变量
$# 是传给脚本的参数个数$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
bracket
$(command) command substitution
${parameter} parameter expansing
$ animal=cat
$ echo $animals
# No such variable as “animals”.
$ echo ${animal}s
cats
$ echo $animal_food
# No such variable as “animal_food”.
$ echo ${animal}_food
cat_food
$ echo "$animal"s
cats
$ plural=s
$ echo $animal$plural
cats
Bracket
parentheses or “round brakets”
((
arithmetic operations
array=(1 2 3)
echo ${array[1]}
2
“sqaure brackets” or “box brackets”
test
and [
builtins
[[
more functions. support && || =~(re)
braces or “curly brackets”
- parameter expansion
- truncate the contents
- siimilar to
sed
- iterate
$ echo f{oo,ee,a}d
food feed fad
$ mv error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace expression
expands to "mv error.log error.log.OLD")
$ for num in {000..2}; do echo "$num"; done
000
001
002
$ echo {00..8..2}
00 02 04 06 08
$ echo {D..T..4}
D H L P T
“angle brakets”
>
redirection
>>
redirection at the end the orginal file
Various symbol
vertical bar
|
refereed to as a “pipe”, direct output from the first command into the input for the second command
||
if the first command succeed the second will never be executed or opposite
&
&
同时执行多条命令,不管命令是否执行成功
&&
可同时执行多条命令,当碰到执行错误的命令时,将不再执行后面的命令。如果一直没有错误的,则执行完毕。
redirection
[HoWTO][http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html]
Referee
stackoverflow
shell中各种括号的作用
shell 编程基础
shell command example
BASH Programming