Reputation: 587
I need to print the argument number of which user types in. No matter what I do I always get just an empty line
echo "Give argument number"
read number
allV=$@
echo ${allV[$number]}
what is wrong with this few lines? Even if I start the script with a few arguments and I just manually write sth like"
echo ${allV[1]}
again all I get is an empty line.
Upvotes: 2
Views: 72
Reputation: 15461
To handle $@
as an array, just change it to ("$@")
:
echo "Give argument number"
read number
allV=("$@")
echo ${allV[$number-1]}
Upvotes: 1
Reputation: 241881
Bash lets you use an indirect reference, which works also on numbered parameters:
echo "${!number}"
It also lets you slice the argument list:
echo "${@:$number:1}"
Or you could copy the arguments into an array:
argv=("$@")
echo "${argv[number]}"
In all cases, the quotes are almost certainly required, in case the argument includes whitespace and/or glob characters.
Upvotes: 2