shell编程之算术扩展(引号、命令替换、算术扩展)

1、单引号 、双引号、反引号的区别

单引号:忽略所有特殊字符

双引号:忽略大部分特殊字符($  `等字符除外)

[root@tlinux shell]# echo '*'
*
[root@tlinux shell]# echo *          输出目录下所有文件
02.sh 03.sh o1hello.sh text.sh
[root@tlinux shell]# x
=* [root@tlinux shell]# echo $x 02.sh 03.sh o1hello.sh text.sh [root@tlinux shell]# echo "$x" * [root@tlinux shell]# echo '$x' 忽略所有特殊字符 $x

反引号:命令替换(将一个命令的标准输出插入到命令的任何位置)

$(): 同样是命令替换

命令替换可以嵌套(如果使用反引号,则内部的反引号必须用反斜杠来转义)  :echo  `basename \`pwd\``       echo $(basename $(pwd))

basename 命令:求路径中最后一项名称

dirname 命令:求路径中路径前面的名称

[root@tlinux shell]# echo `pwd`
/shell
[root@tlinux shell]# echo $(pwd)
/shell
[root@tlinux shell]# basename /hh/aa
aa
[root@tlinux shell]# dirname /hh/aa/bb
/hh/aa
\[root@tlinux shell]# basename  `pwd`
shell
[root@tlinux shell]# dirname `pwd`
/
[root@tlinux shell]# echo `basename \`pwd\``
shell
[root@tlinux shell]# echo $(basename $(pwd))
shell

2、算术运算符

基本算术运算符与C语言一模一样

算术扩展:$[] 

[root@tlinux shell]# n=5;echo $[$n+1]
6
[root@tlinux shell]# echo n+1
n+1
[root@tlinux shell]# echo $n+1
5+1

$(())等价于$[]  

[root@tlinux shell]# echo $(($n+2))
7
[root@tlinux shell]# echo $(($n*2))
10

(()) :整体表达式 不可赋值   是一个独立的语句

[root@tlinux shell]# ((n+=1))
[root@tlinux shell]# echo $n
6
[root@tlinux shell]# echo ((n+=1))
bash: syntax error near unexpected token `('
[root@tlinux shell]# r=((n+=1))
bash: syntax error near unexpected token `('
[root@tlinux shell]# r=$[$n+1]
[root@tlinux shell]# echo $r
7

expr  :符号两边一定要有空格

[root@tlinux shell]# expr 4 + 5
9
[root@tlinux shell]# r= `expr 4 * 5`
expr: syntax error
[root@tlinux shell]# r=`expr 4 \* 5`   *对shell有特殊含义  echo   $*
[root@tlinux shell]# echo $r
20

let  是shell内置命令

[root@tlinux shell]# let n=n+_1
[root@tlinux shell]# 
[root@tlinux shell]# let n=n+1
[root@tlinux shell]# echo $n
6
[root@tlinux shell]# let "n= n+ 1"
[root@tlinux shell]# echo $n
7
[root@tlinux shell]# let n=n +1
[root@tlinux shell]# echo $n
7
[root@tlinux shell]# let n= n +1
bash: let: n=: syntax error: operand expected (error token is "=")
[root@tlinux shell]# let "n= n+1"
[root@tlinux shell]# echo $n
8

 

转载于:https://www.cnblogs.com/wsw-seu/p/10816225.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值