0

I have problem with getting value of parameter when I have variable with some value. What I mean is:

Example:

./script 12 13 14 15 16

value=5
echo $value #now I see 5
$"$value" #now I want to get 16 but I don't know how to do it?

4 Answers 4

4

Use indirection:

echo "${!value}"

Quotes aren't necessary for the value 16, but should be used in case the variable contains special characters.

Sign up to request clarification or add additional context in comments.

Comments

1

try this as well:

value=5              #
echo "$value"        # 5 
echo ${@:$value:1}   # give you 1 arg starting from $value in the arg list     

Comments

0

you need to dereference that variable

value=5
echo "$value"      # 5
echo "${!value}"   # will give you $5 or in your example 16

Comments

0

bashisms are inherently non-portable. If you rely on ${!...} to evaluate the expression, your script will only run in bash. This may not be an issue, but it is an issue if the author of the script is blissfully unaware of the lack of portability. This sort of thing is trivial to do without relying of bashisms. If you want to evaluate a string, use eval:

eval echo \$$value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.