如何从bash中的变量中获取值

时间:2022-04-28 15:42:26

I have bash script in which I am passing number of arguments and want to get argument at index specified

我有bash脚本,其中我传递了多个参数,并希望在指定的索引处获取参数

#! /bin/bash

index=$1
echo "argument at index $1 is" $[$index]

./temp.sh 3 1 2 5 6
argument at index 3 is 3

but I want output like
argument at index 3 is 2

1 个解决方案

#1


1  

You can use indirect variable referencing:

您可以使用间接变量引用:

#! /bin/bash

index="$1"
echo "argument at index: $index is ${!index}"

OUTPUT: (with your command line)

输出:(使用您的命令行)

argument at index: 3 is 2

#1


1  

You can use indirect variable referencing:

您可以使用间接变量引用:

#! /bin/bash

index="$1"
echo "argument at index: $index is ${!index}"

OUTPUT: (with your command line)

输出:(使用您的命令行)

argument at index: 3 is 2