Reputation: 11
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.
Upvotes: 1
Views: 223
Reputation: 819
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
Upvotes: 1
Reputation: 96258
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
Upvotes: 0
Reputation: 824
This will give you an array with the arguments: args=("$@")
And you can call them like this: echo ${args[0]} ${args[1]} ${args[2]}
Upvotes: 7