I have some directories in a location, that location's absolute path is stored in PROJECT_HOME. Now, I am trying to do the following in a script:

TARGET_LIST="bin lib scripts deployment"
tar -czf project.tar.gz -C "$PROJECT_HOME" "$TARGET_LIST"

but tar gives me the following:

tar: bin lib scripts deployment: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors

The tar command works if I substitute the values of TARGET_LIST (tar -czf project.tar.gz -C "$PROJECT_HOME" lib bin scripts deployment), but doesn't when I use the variable.

What am I doing wrong?


By putting $TARGET_LIST between quotes you are defining a single argument with spaces. tar is looking for a single file/folder named 'bin lib scripts deployment'.

Try with

tar -czf project.tar.gz -C $PROJECT_HOME $TARGET_LIST

without the quotes

EDIT If you have spaces in your file or directory names you need to use arrays (as suggested by Ignacio)


TARGET_LIST=(bin lib scripts deployment "a directory with spaces")
tar -czf project.tar.gz -C $PROJECT_HOME "${TARGET_LIST[@]}"
| improve this answer | |

You're having the opposite of the problem described in BASH FAQ entry #50. Fortunately the fix is the same: use an array.

| improve this answer | |

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.