1

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

#! /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 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.