Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 1 down vote accepted

You could use an array:

BASES[0]=...
BASES[1]=...
BASES[2]=...
# 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
add comment

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
add comment

You could use parallel twice:

seq 1 3 | parallel echo '$BASE'{} | parallel cp -a {} "$DEST"

Should work with filenames containing spaces. I haven't tried with filenames containing tabs or newline.

share|improve this answer
add comment

Your Answer

 
discard

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.