Search code examples
bashbrace-expansionparameter-expansion

Can't combine brace expansion with parameter expansion in bash


$ set a b c
$ echo $1 $2 $3
a b c
$ echo ${1..3}
-bash: ${1..3}: bad substitution

I want to echo $1 $2 $3 with brace expansion. But it doesn't work.

The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

According to the bash manual, brace expansion is performed before parameter expansion.

So I think bash should perform brace expansion on ${1..3} first, convert it to echo $1 $2 $3, and then perform parameter expansion.

However, the fact is that bash complains ${1..3} is a bad substitution. Why is that?

PS: Thank you guys!All you answers are wonderful.But I think

Ignacio Vazquez-Abrams's answer is closer to what I want.


Solution

  • It thinks that the braces are part of the parameter substitution since it follows a dollar sign, and "$1..3" is an invalid variable name.

    Try "${@:1:3}" instead.