1

I would like to print a few arguments given in the console
but I would like to print the argument with a number given from a integer

declare -i I=2
declare -i I=4

I would like to print the arguments number 2 and number 4 how can I do that without using the following if statements

if [ $I -eq 2 ]; then
echo $2
fi 


What I am searching for is somethink like this
echo $($I) #first access $I, which is 4 and
# then print $4, which is the 4th argument

user3597432
  • 131
  • 6

2 Answers2

1

Looks like you're looking for variable indirection. Use like this:

func() {
    p=4
    echo "${!p}"
}

TESTING:

func aa bb cc dd ee
dd
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

To see what each argument is assigned to you can use this loop.

for n in $(seq 1 $#)
do
  echo $n ${!n}
done
John C
  • 4,276
  • 2
  • 17
  • 28