Reputation: 5
I have a few strings in $1 to $#. I want to perform a simple arithmetic expression and echo out the value of $ based on the # of the expression.
eg. set file1 file2 file3 file4
I want to echo $2 (file2) but using an arithmetic expression "echo $$((3-1))", which I thought would resolve to $2 but bash just throws an error.
Upvotes: 0
Views: 124
Reputation: 531758
Using the substring expansion operator:
$ set -- file1 file2 file3 file4
$ echo ${@:$((3-1)):1}
file2
Upvotes: 0
Reputation: 123550
Use variable indirection:
set -- foo bar baz
var=$((1+1))
echo "${!var}"
This will print the value of $2
, i.e. "bar".
Upvotes: 3