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
More Related Contents:
- How do I parse command line arguments in Bash?
- Using getopts to process long and short command line options
- Passing parameters to a Bash function
- Accessing bash command line args $@ vs $*
- Bash command line and input limit
- Propagate all arguments in a bash shell script
- Meaning of “[: too many arguments” error from if [] (square brackets)
- Check number of arguments passed to a Bash script
- What is the difference between “$@” and “$*” in Bash? [duplicate]
- How to access command line arguments of the caller inside a function?
- How to write a bash script that takes optional input arguments?
- find: missing argument to -exec
- Retrieving multiple arguments for a single option using getopts in Bash
- How to pass arguments to Shell Script through docker run
- Why do I get “/bin/sh: Argument list too long” when passing quoted arguments?
- How can I use long options with the Bash getopts builtin?
- build argument lists containing whitespace
- How to substitute quoted, multi-word strings as arguments?
- How do I find the number of arguments passed to a Bash script?
- How to change a command line argument in Bash?
- How do I prompt a user for confirmation in bash script? [duplicate]
- Counting lines or enumerating line numbers so I can loop over them – why is this an anti-pattern?
- Why doesn’t my terminal output unicode characters properly?
- Multi Level Bash Completion
- In Bash test if associative array is declared
- Left side of pipe is the subshell?
- How to redirect a program that writes to tty?
- How to read variables from file, with multiple variables per line?
- What is the proper way to test a Bash function’s return value?
- How to loop over files in natural order in Bash?