【linux编程】生信编程-shell脚本编程-数学运算

【linux编程】生信编程-shell脚本编程-数学运算_第1张图片
shell编程

Shell 脚本中的数学运算

bash shell 的基础运算主要有4种形式:

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

实例:

#!/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 ))

结果:

#!/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 ))

推荐涉及运算,统一使用,方便区分
$[ expression ]

你可能感兴趣的:(【linux编程】生信编程-shell脚本编程-数学运算)