0

I want to get the arg of a command from a var that has the index in it of the arg that I want. Something like this

# command in terminal, `foo -r -f value_wanted`
index="3"
var=$"$index"
echo $var ## expected output `value_wanted`

I know I can just call it by $3 but that index I have is in the variable.

3

You can achieve that by using the following notation:

echo "${!index}"

If you want to process positional arguments, I suggest to use getopt (not getopts), though.

  • perfect, that did what was expected. Side note, this was to implement from unix.stackexchange.com/a/257412/47672 , so I'm "fast-forwarding" the cursor in a way and getting the arg I need for the options, then rewinding it and doing the loop I need on the options. – jeremy.bass Jan 27 '16 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.