Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers

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)

Example

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

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
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.