Bashの算術構文

Bashの算術構文

次の算術値エラーが発生します。

#!/bin/bash
n=0
line_count=9
line_count=$(line_count)/3
echo $line_count
exit 0

予想結果は3

[]$ ./test.sh
./test.sh: line 4: line_count: command not found
/3
[]$ more test.sh

ベストアンサー1

補う@Kusalanandaの返信、標準構文に加えて、次のようになりますsh

line_count=$((line_count / 3))

bashから継承された次の構文を使用することもできます(kshでも利用可能zsh)。

  • ((行数 = 行数/3))
  • ((行数/= 3))
  • line_count/=3にしておきます
  • 組版-i 行数 = 行数/3

bash(およびzsh)は以下もサポートしています。

  • 行数=$[行数/3]

以前のPOSIX以前のBourne / Almquistの場合sh

line_count=`expr "$line_count" / 3`

おすすめ記事