shell 变量和引用

http://www.91linux.com/html/article/shell/20081231/15182.html

1 变量
1.1 空值变量
1) 没有赋值的情况
2) 显示赋值 a)x= b) x='' c=""
=两边是不能有空格的

1.2 文件名与变量 把文件名修改成 文件名+x,可用 mv $filename {filename}x
1.3 内部整数算术运算
$((expression)) 如:
[pvg@server ~]$ i=2
[pvg@server ~]$ echo $((i+1))
3
[pvg@server ~]$ echo $((i*2))
4

但是较老的shell却不支持内部整数算术操作,所以我们必须用expr命令来替换。expr可以识别常见的操作,包括+(加)、-(减)、*(乘)、/(除)、%(求模)。
[pvg@server ~]$ i=1
[pvg@server ~]$ j=$(expr $i + 1)
[pvg@server ~]$ echo $j
2

[pvg@server ~]$ expr 5 /* 2 乘法需要注意的地方
10
注意:expr 操作是有空格的,如:
[pvg@server ~]$ expr 10+1
10+1
2 引用
2.1 单引号
shell会保留单引号中的特殊字符。
[pvg@server test]$ echo '$filename'
$filename
[pvg@server test]$ echo 'one two three'
one two three

2.2 双引号
双引号和单引号差不多,双引号忽略大多数字符。具体来说,只是美元符号、反引号、反斜杠3中不被忽略。
a)
[pvg@server test]$ x=*
[pvg@server test]$ echo '$x'
$x
[pvg@server test]$ echo "$x"
*
[pvg@server test]$ echo $x
name name2 number test

b) 要赋值给add是单引号或双引号是没有区别的。
[pvg@server test]$ add="39 East 12th Street
> New York, N.Y. 10003"
[pvg@server test]$ echo $add
39 East 12th Street New York, N.Y. 10003
[pvg@server test]$ echo "$add"
39 East 12th Street
New York, N.Y. 10003

3)可以用双引号隐藏shell单引号,反之亦然。
[pvg@server test]$ word="'hello,'he said"
[pvg@server test]$ echo $word
'hello,'he said

2.3 反斜杠
1) 用反斜杠续行
[pvg@server test]$ x=one/
> two
[pvg@server test]$ echo $x
onetwo

2) 双引号中的反斜杠
用反斜杠去掉特殊字符的意义。
[pvg@server test]$ x=5
[pvg@server test]$ echo "/$x"
$x
[pvg@server test]$ echo "The Value of x is $x"
The Value of x is 5

2.4 命令替换
命令替换是指shell能够将一个命令的标准输出插在一个命令行中的任意位置的功能。其实现可以通过把shell命令用反引号括起来或$(...)结构。
1)
[pvg@server test]$ echo The date and time is:`date`
The date and time is:Web Aug 30 18:01:04 EAT 20
2)[pvg@server test]$ echo The date and time is:$(date)
The date and time is:Web Aug 30 18:01:04 EAT 20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值