1
1

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 3 years have passed since last update.

【shellscript】コマンドライン引数取り扱いメモ

Last updated at Posted at 2021-06-08

shellscriptのコマンドライン引数の取り扱いについて、
備忘録としてまとめておく

test.sh
###################
# 一番目の引数を取得
###################
arg_one=$1
echo "一番目の引数を取得"
echo $arg_one

###################
# 引数をすべて取得 
###################
args_ex=$@
echo "引数をすべて取得 "
echo $args_ex

#####################
# 2番目以降の引数を取得
#####################
args_ex2=${@:2}
echo "2番目以降の引数を取得"
echo $args_ex2

########################
# 2番目以降の引数を3つ取得
########################
args_ex3=${@:2:3}
echo "2番目以降の引数を3つ取得"
echo $args_ex3

###########################
# 引数をすべてを配列として取得
###########################
args_array=("${@}")
echo "引数をすべてを配列として取得"
for args_value in ${args_array[@]}; do
  echo $args_value
done

実行テスト

$ sh test-args.sh a b c d e

# 実行結果
一番目の引数を取得
a
引数をすべて取得 
a b c d e
2番目以降の引数を取得
b c d e
2番目以降の引数を3つ取得
b c d
引数をすべてを配列として取得
a
b
c
d
e
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?