BeeOnRope
BeeOnRope

Reputation: 64925

Brace expansion into a single argument

I am calling a bash function like so:

func "foo-A-bar foo-B-bar foo-C-bar"

I would like to replace the above with a brace expansion that evaluates to the same single argument. The following doesn't work:

func foo-{A,B,C}-bar

because that's three separate arguments, not one argument with space-separated values. That is, it is equvalent to:

func foo-A-bar foo-B-bar foo-C-bar

which is not the same thing as the original call.

Upvotes: 1

Views: 302

Answers (3)

chepner
chepner

Reputation: 531165

Create an array first.

args=(foo-{A,B,C}-bar)
func "${args[*]}"

Upvotes: 2

Inian
Inian

Reputation: 85580

You can construct the brace expansion result into a positional argument list and expand the arguments as a single expanded string.

set -- foo-{A,B,C}-bar
func "$*"

The set command with the result of the brace expansion, sets the arguments from $1 as foo-A-bar and $2 as foo-B-bar respectively. With these positional arguments in-place, the array expansion $* causes them arguments to be combined as a single string joined by the default IFS value ( a single white-space )

Upvotes: 2

anubhava
anubhava

Reputation: 785146

You may use command substitution:

func "$(echo foo-{A,B,C}-bar)"

which is equivalent of using:

func "foo-A-bar foo-B-bar foo-C-bar"

Upvotes: 0

Related Questions