BASH scripting: n-th parameter of $@ when the index is a variable? BASH scripting: n-th parameter of $@ when the index is a variable? shell shell

BASH scripting: n-th parameter of $@ when the index is a variable?


You can use variable indirection. It is independent of arrays, and works fine in your example:

n=2echo "${!n}"

Edit: Variable Indirection can be used in a lot of situations. If there is a variable foobar, then the following two variable expansions produce the same result:

$foobarname=foobar${!name}


Try this:

#!/bin/bashargs=("$@")echo ${args[1]}

okay replace the "1" with some $n or something...


The following works too:

#!/bin/bashn=2echo ${@:$n:1}