Domanda

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

È stato utile?

Soluzione

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

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

TESTING:

func aa bb cc dd ee
dd

Altri suggerimenti

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

for n in $(seq 1 $#)
do
  echo $n ${!n}
done
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top