ループ制御 「for」文

シェルスクリプトにおける for文の基本的な書式

for 変数 in リスト
do
    処理
done
  • シェルスクリプトの for文は in の後ろに複数の値を並べて記述する
  • 処理内容は 「do」 ~ 「done」 の間に記述する

#! /bin/bash

for val in red blue green yellow
do
    echo $val
done

実行結果

[riceplanting@localhost public_html]$ ./forTest
red
blue
green
yellow

スクリプト実行時に指定される複数の引数

ひとまとめに「 $@ 」という変数で取り込むことができる

#! /bin/bash

for val in "$@"
do
    echo $val
done

引数に red bule green yellow を指定して実行した結果

[riceplanting@localhost public_html]$ ./forTest red blue green yellow
red
blue
green
yellow