Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do brace expansion on variables? [duplicate]

Consider the following script:

#! /bin/bash -e

echo {foo,bar}
EX={foo,bar}
echo ${EX}

The output of this script is:

foo bar
{foo,bar}

I would like the the echo command to perform brace expansion on ${EX}. Thus, I would like to see an output of

foo bar
foo bar

I want to create a script where the user can supply a path with curly brackets where every expanded version of it is copied.

Something like this:

#! /bin/bash -e

$SOURCES=$1
$TARGET=$2

cp -r ${SOURCES} ${TARGET}

How can I achieve this?

like image 701
Johannes Dorn Avatar asked Oct 15 '25 08:10

Johannes Dorn


2 Answers

This is a way:

ex=({foo,bar,baz})
echo ${ex[@]}
foo bar baz
like image 102
Jahid Avatar answered Oct 18 '25 11:10

Jahid


See man bash:

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.

As you see, variable expansion happens later than brace expansion.

Fortunately, you don't need it at all: let the user specify the braced paths, let the shell expand them. You can then just

mv "$@"

If you need to separate the arguments, use an array and parameter expansion:

sources=("${@:1:$#-1}")
target=${@: -1}
mv "${sources[@]}" "$target"
like image 35
choroba Avatar answered Oct 18 '25 11:10

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!