posted by 귀염둥이채원 2021. 4. 15. 04:11

---------------------------------------------------------

# 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