Some Name
Some Name

Reputation: 9521

Processing each of the second and further arguments

I have a bash scripts accepting 1 one more arguments. I want to handle each of the second and further arguments separately. Here is my attempt:

SECOND_PLUS_ARGS="${@:2}"

for arg in "${SECOND_PLUS_ARGS[@]}"; do
    echo "arg = $arg"
done

Running it as ./script.sh 1 2 3 4 the following output is printed:

$ ./script.sh 1 2 3 4                                                                                                                                                                        
arg = 2 3 4      

I expected:

$ ./script.sh 1 2 3 4                                                                                                                                                                        
arg = 2
arg = 3
arg = 4

Is there a way to fix the script?

Upvotes: 0

Views: 29

Answers (1)

user1934428
user1934428

Reputation: 22225

It does not make much sense to create SECOND_PLUS_ARGS as scalar and then use it as array. Create it as array:

SECOND_PLUS_ARGS=("${@:2}")

Upvotes: 1

Related Questions