---------------------------------------------------------
# argument example
---------------------------------------------------------
#!/bin/bash
# echo arguments to the shell
echo $1 $2 $3 ' -> echo $1 $2 $3'
# We can also store arguments from bash command line in special array
args=("$@")
# echo arguments to the shell
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'
# use $@ to print out all arguments at once
echo $@ ' -> echo $@'
# use $# variable to print out
# number of arguments passed to the bash script
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#'
# with for
for i in $@; do
echo "$i"
done
# output
$ bash test.sh input1 input2 input3 input4 input5
input1 input2 input3 -> echo $1 $2 $3
input1 input2 input3 -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
input1 input2 input3 input4 input5 -> echo $@
Number of arguments passed: 5 -> echo Number of arguments passed: $#
input1
input2
input3
input4
input5
'쉘스크립트' 카테고리의 다른 글
[Shell Script] read를 이용한 입력값 받기 (0) | 2021.04.15 |
---|---|
[Shell Script] 배열(Array) (0) | 2021.04.15 |
[Shell Script] 변수(Variable) (0) | 2021.04.15 |
[Shell Script] echo와 printf를 사용한 출력 (0) | 2021.04.15 |
[Shell Script] 쉘스크립트 기본 및 학습자료 (0) | 2021.04.15 |