1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプト bash for 文 算術式

Posted at

bash では C言語のような for 文で記述が可能。

for-bash-example1.sh
total=0
for ((i = 1; i <= 12; i++))
do
  ((total += i))
done
echo "${total}"

do - done を { } で置き換えることができる。

for-bash-example2.sh
total=0
for ((i = 1; i <= 12; i++)) {
  ((total += i))
}
echo "${total}"

シェル変数(total)の初期化をfor文の中でできる。
最後の total の表示を算術式展開$((xxx))で行うことができる。

for-bash-example3.sh
for ((total = 0, i = 1; i <= 12; i++)) {
  ((total += i))
}
echo "${total}"
echo "$((total))"

iのインクリメントを省略したら、無限ループ。

for-bash-example4.sh
for ((i = 0; i <= 1;))
do
  echo "Test:${i}"
done

bash でしか動かないため、積極的に使わなくて良いと思う。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?