I have the following definitions in my bash script:

DATE_SUFIX=$(date +%F_%H-%M-%S)
TAR_CMD="tar czPf "

and I create a dir list like this:


Then I iterate this list with this loop:

mkdir $TMP_BKP
for ix in ${!FOLDER_LIST[*]}
    CMD=$TAR_CMD$TMP_BKP$(basename ${FOLDER_LIST[$ix]})".tar.gz "${FOLDER_LIST[$ix]}"/* "$EXCLUDE
    echo $CMD

I also have some code to generate the $EXCLUDE part. I does it the same way. But the relevant part of code for this question is that "${FOLDER_LIST[$ix]}"/* part in CMD expands to:

"/home/user/mybackupfolder/foo /home/user/mybackupfolder/bar ..."

the problem is that one of the expanded sub-folders has spaces in it, generating:

"/home/user/mybackupfolder/foo /home/user/mybackupfolder/bar /home/user/mybackupfolder/my spaced dir"

and that breaks the following operations. How can I make it expand to this? -

"'/home/user/mybackupfolder/foo' '/home/user/mybackupfolder/bar' '/home/user/mybackupfolder/my spaced dir'"
  • How do you get foo and bar in your list?
    – roaima
    Apr 11, 2015 at 23:56
  • @roaima edited the question Apr 12, 2015 at 5:55
  • How does FOLDER_LIST relate to MY_DIR? None of FOLDER_LIST is a folder that contains a space.
    – roaima
    Apr 12, 2015 at 7:21
  • @roaima oh sorry. When I re-read the question I realized some part of what I originaly wrote was missing. Maybe I accidentally delete it when I was editing. It's complete now. :) Apr 12, 2015 at 14:18
  • 2
    Looking at that code it seems to me that you would benefit from reading BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!
    – roaima
    Apr 12, 2015 at 14:30

1 Answer 1


(Answer culled from comment thread)

This seems to be a case where quotes are either missing or wrongly applied. You cannot put quotes into a string and have the shell to interpret the result as a quoted string.

Please could I suggest that you read BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail! and see if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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