Why doesn't ${@:-1} return the last element of $@?
bash
Solution
`${parameter:-word}` is a type of parameter expansion:
`${parameter:-word}`
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
So `${@:-1}` is interpreted as:
- If `$@` is unset or null, substitute with `1`
- Else leave `$@` as it is
Since the latter is true, `echo` prints `$@` in all its glory
The Fix:
Put some space between `:` and `-`:
$ echo ${@: -1}
f
Problem
I thought to post up a Q&A on this as I did not find anything similar. If it already exists, please mark this as a duplicate. The following code, running under Bash shell, doesn't work (should return just `f`, the last (-1-th) item in `$@`): ``` $ set -- a b c d e f $ echo ${@:-1} a b c d e f ```