Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following script to copy multiple files into one folder:

{ echo $BASE1; echo $BASE2; echo $BASE3; } | parallel cp -a {} $DEST

Is there any way to use only one echo $BASE with brace expansion?

I mean something like this:

{ echo $BASE{1..3} } | parallel cp -a {} $DEST
share|improve this question
up vote 1 down vote accepted

You could use an array:

# or BASES+=(...)
# or BASES=(foo bar baz)
echo "${BASES[@]}" | parallel cp -a {} $DEST

To make it safer (spaces and newlines in the variable in particular), something like this should work more reliably:

printf "%s\0" "${BASES[@]}" | parallel -0 cp -a {} "$DEST"

Note: arrays aren't in POSIX, this works with current versions of bash and ksh though.

share|improve this answer
Arrays and portability... – qdii Oct 27 '12 at 5:40
I´m going to try this option. Thanks. – damianjb Oct 27 '12 at 19:47
@damianjb In zsh, you can save a bit of typing: print -N $BASES | parallel -0 cp -a {} $DEST – Gilles Oct 27 '12 at 23:41
@Gilles I'll keep that in mind. Thanks. – damianjb Oct 28 '12 at 14:52

You can use printf instead of echo, to get the newlines you need:

 env printf "%s\n" $BASE{1..3} | parallel cp -a {} $DEST
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.