Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

The following works from the command line, but fails as script:

gunzip -c /disk1/hadoop/gunzip_tmp/in/part-00006.gz >> /disk1/hadoop/gunzip_tmp/outfile.000

The following script lines:

UZPCMD="gunzip -c "$TMPDIR"/in/"$FILEONLY" >> "$TMPDIR"/outfile.000"
echo $TMPDIR
echo $UZPCMD

produce the following output - the gunzip command line looks as it should:

gunzip -c /disk1/hadoop/gunzip_tmp/in/part-00006.gz >> /disk1/hadoop/gunzip_tmp/outfile.000

However, issuing the command line from the script:


sends line after line of the unzipped file to stdout (as if the redirection were being ignored).

share|improve this question
up vote 4 down vote accepted

You cannot specify the redirection as part of the variable string value.

The shell does some additional evaluation on input when you enter something directly on the command line, and that doesn't happen for string values. >> is just another argument to gunzip.

$ CMD="echo >> foo"
$ $CMD
>> foo

You can use eval instead. In the example, eval $CMD would correctly redirect echo output to the file foo. Note that this can introduce problems with unexpected input.

share|improve this answer
eval $UZPCMD worked like a charm. Thanks, Daniel! – MarkP Feb 14 '12 at 21:53
@MarkP: in general, you should enclose variable substitutions in double-quotes to prevent unwanted extra parsing, e.g. UZPCMD="gunzip -c $TMPDIR/in/$FILEONLY >> $TMPDIR/outfile.000" and eval "$UZPCMD" -- it doesn't matter in this particular case, but is a good habit to get into. – Gordon Davisson Feb 14 '12 at 23:24

Your Answer


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.