1

I need to store command line arguments passed in an array

my Command is

./test1.sh 2 4 6

Now i need to store 2 4 6 in an array and im using..

s1=$#     
"it tells how many arguments are passed."


for (( c=1; c<=$s1; c++ ))

do

     a[$c]}=${$c}

I have written ${$c} for taking the arguments value but it is showing bad substition.

0

3 Answers 3

7

This will give you an array with the arguments: args=("$@")

And you can call them like this: echo ${args[0]} ${args[1]} ${args[2]}

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help. Could you mark an accepted answer, so the issue wont stay in the "unanswered" section?
1

bash variable can be indirectly referenced by \$$VARNAME or by ${!VARNAME} in version 2 so your assignment statement must be :

a[$c]=${!c} 

or

eval a[$c]=\$$c

Comments

0

You can always pick the first argument and drop it. Use $1 and shift.

c=1
while [ $# -gt 0 ]; do
    a[$c]=$1
    c=$((c+1))
    shift
done

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.