多个语句可以使用 {} 或者 ()组合. 包含在 {} 里的在当前shell中执行. 包含在 ()里的在子shell中执行. {后面必须有空格。{}中的每个命令必须以分号结束。
例:想在cat temp的输出前加一行
$ cat temp
The rain in Spain
falls mainly on the plain
$ echo "This is file temp:" ; cat temp | nl
This is file temp:
1 The rain in Spain
2 falls mainly on the plain
$ { echo "This is file temp:"; cat temp ;} | nl
1 This is file temp:
2 The rain in Spain
3 falls mainly on the plain
文件名替换
* 匹配任意0或者多个字符
? 任意一个字符
[]匹配一个范围内的单个字符[am] [a-z]
! 可以在[]内表示取反 [!d]*
命令替换
$(command)
`command` #兼容Bourne Shell
直接文件输入
$(
ffff
oooo
$xxx=$(
ffff oooo
算数操作
$((arithmetic-expression))
$ echo $((3+5))
8
变量属性
typeset -attribute variable=value
或者
typeset -attribute variable
小写/大写属性-l/-u
$ typeset -l MYSYS=ASPD
$ print $MYSYS
aspd
只读属性 -r
$ typeset -r BACKUP_PATH=/backup
整数属性-i
$ typeset -i NUM=1
$ typeset -i NUM=$(who | wc -l)
$ typeset --i NUM=abc
/bin/ksh: NUM: bad number
浮点数属性-E, -F
-E 有效位数
-F 精度
$ typeset --E5 X=123.456
$ print $X
123.46
$ typeset --F5 X=123.456
$ print $X
123.45600
自动export属性-x
$ typeset --x PATH=$PATH:/lbin
多重属性
$ typeset --ix TMOUT=3000
变量扩展
${#variable}
length of variable
${variable:?-word}
value of variable if set and not null, else print word
${variable:= word}
value of variable if set and not null, else variable is set to word, then expanded
${variable:+word}
value of word if variable is set and not null, else nothing is substituted
${variable:?}
value of variable if set and not null, else print 'variable: parameter null or not set'
${variable:?word}
value of variable if set and not null, else print value of word and exit
${variable #pattern}
value of variable without the smallest beginning portion that matches pattern
${variable ##pattern}
value of variable without the largest beginning portion that matches pattern
${variable%pattern}
value of variable without the smallest ending portion that matches pattern
${variable%%pattern}
value of variable without the largest ending portion that matches pattern
${variable//pattern1/pattern2}
replace all occurrences of pattern1 with pattern2 in variable
数组
$ X=A
$ X[1]=B #X[0]仍然是A
数组赋值和声明
variable[0]=value variable[1]=value. . . variable[n]=value
或
set -A variable value0 value1 . . . valuen
或
typeset variable[0]=value variable[1]=value. . .variable[n]=value
数组变量
${array}, $array
array element zero
${array[n]}
array element n
${array[n+2]}
array element n+2
${array[$i]}
array element $i
${array[*]}, ${array[@]}
all elements of an array
${#array[*]}, ${#array[@]}
number of array elements
${#array[n]
length of array element n
${!array[*]}, ${!array[@]}
all initialized subscript values
${!array[*]:n:x}
x array elements starting withelement n
${!array[@]:n}
all array elements starting with element n