Super User is a question and answer site for computer enthusiasts and power users. 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 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?

share|improve this question
up vote 1 down vote accepted

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[@]}"
share|improve this answer
Ah, right. It worked, thanks. :-) – Saadat Oct 12 '11 at 7:06
@Saadat: Only until you hit something with whitespace. – Ignacio Vazquez-Abrams Oct 12 '11 at 7:08

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

share|improve this answer
Thanks I updated my answer. – Matteo Oct 12 '11 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .