I'm new to bash and I'm working on script that traverses the tar.gz file archive and in each file changes a string specified to an another string. Args of script: name of archive, searched string, target word. My problem is that when archive name contains a space (e.g. I run script with following args: > change_strings.sh "/tmp/tmp.m7xYn5EQ2y/work/data txt" a A) I have following error: on line if [ ! -f $filename ] ; then [: data: binary operator expected, dirname: extra operand `txt'. Here is my code:

  DIR=$(dirname ${VAR})
  cd "$DIR"

if [ ! -f $filename ] ; then
echo "no such archive" >&2
exit 1

if ! tar tf $filename &> /dev/null; then
echo "this is not .tar.gz archive" >&2
exit 1

dir=`mktemp -dt 'test.XXXXXX'`
tar -xf $filename -C $dir  #extract archive to dir
cd $dir #go to argument directory


for f in $FILES
sed -i "s/$2/$3/g" "$f"

tar -czf $filename * #create tar gz archive with files in current directory
mv -f $filename $cdir"/"$filename #move archive
rm -r $dir #remove tmp directory

marked as duplicate by Ramesh, goldilocks, Gilles bash Oct 26 '14 at 16:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You should put your $filename in double quotes: "$filename"

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