【linux編程】生信編程-shell腳本編程-數(shù)學(xué)運(yùn)算

shell編程

Shell 腳本中的數(shù)學(xué)運(yùn)算

bash shell 的基礎(chǔ)運(yùn)算主要有4種形式:

  • $(( expression ))
  • $[ expression ]
  • expr operation
  • 使用 let

實(shí)例:

#!/bin/bash
set -e
set -u
a=20
echo "a = $a"
b=6
echo "b = $b"
c=4.5
echo "c = $c"
result=$(( a + b ))
echo "The result of '\$(( a + b ))' is $result"
result1=$[ a - b ]
echo "The result of '\$[ a - b ]' is $result1"
result2=`expr $a \* $b`
echo "The result of '\`expr \$a \* \$b\`' is $result2"
let "result3 = $a + $b"
echo "The result of 'let \"result3 = \$a + \$b\"' (result3) is $result3"
result4=$(expr $a / $b )
echo "The result of '\$(expr \$a / \$b )' is $result4"
result5=$(( a + c ))

結(jié)果:

#!/bin/bash
set -e
set -u
a=20
echo "a = $a"
b=6
echo "b = $b"
c=4.5
echo "c = $c"
result=$(( a + b ))
echo "The result of '\$(( a + b ))' is $result"
result1=$[ a - b ]
echo "The result of '\$[ a - b ]' is $result1"
result2=`expr $a \* $b`
echo "The result of '\`expr \$a \* \$b\`' is $result2"
let "result3 = $a + $b"
echo "The result of 'let \"result3 = \$a + \$b\"' (result3) is $result3"
result4=$(expr $a / $b )
echo "The result of '\$(expr \$a / \$b )' is $result4"
result5=$(( a + c ))

推薦涉及運(yùn)算,統(tǒng)一使用,方便區(qū)分
$[ expression ]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容