Shell 运算符

9 篇文章 0 订阅

Ptw-cwl


“$((运算式))” 或 “$[运算式]

原始计算方式

加法

[root@pwd ~]# a=1+2
[root@pwd ~]# echo $a
1+2
[root@pwd ~]# expr 1+2
1+2
[root@pwd ~]# expr 1 + 2
3
[root@pwd ~]#

减法

[root@pwd ~]# expr 2 - 1
1
[root@pwd ~]# 

乘法

[root@pwd ~]# expr 1 * 2
2
[root@pwd ~]# expr 1 \* 2
2
[root@pwd ~]#

注意:\* 为转义字符

除法
 

[root@pwd ~]# expr 4 / 2
2
[root@pwd ~]# expr 4 \/ 2
2
[root@pwd ~]#

注意:\/ 为转义字符

[root@pwd ~]# a='expr 1 \* 2'
[root@pwd ~]# echo $a
expr 1 \* 2
[root@pwd ~]# a=${expr 1 \* 2}
-bash: ${expr 1 \* 2}: bad substitution
[root@pwd ~]# a=$(expr 1 \* 2)
[root@pwd ~]# echo $a
2
[root@pwd ~]#

使用运算式

[root@pwd shdir]# a=$((2+2))
[root@pwd shdir]# echo $a
4
[root@pwd shdir]# a=$[2+3]
[root@pwd shdir]# echo $a
5
[root@pwd shdir]#

编写加法shell脚本:

[root@pwd shdir]# vim add.sh

#!/bin/bash
sum=$[$1+$2]
echo $sum

运行结果:

[root@pwd shdir]# sh add.sh 3 5
8
[root@pwd shdir]# 

其他的多很相似,读者朋友们可以一一尝试。

Ptw-cwl


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ptw-cwl

谢谢老板的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值