How to get the n
th positional argument in Bash, where n
is variable?
Use Bash's indirection feature:
#!/bin/bash
n=3
echo ${!n}
Running that file:
$ ./ind apple banana cantaloupe dates
Produces:
cantaloupe
Edit:
You can also do array slicing:
echo ${@:$n:1}
but not array subscripts:
echo ${@[n]} # WON'T WORK