0

Consider:

$ FILE_NAME=`(cat somefile | head -1)` | tee -a dump.txt
$ echo $FILE_NAME

$ 
  1. Now, why doesn't the output of (cat somefile | head -1) reach the standard input of tee ..?
  2. If the output reached tee, then it could copy it to dump.txt file and the standard output.
  3. Also the variable $FILE_NAME does not receive the value.

1 Answer 1

3

You probably meant to write

FILE_NAME=`(cat somefile | head -1) | tee -a dump.txt`
echo $FILE_NAME

(or head -1 somefile to get rid of the cat)

The pipe outside ` is more of a logic error. You'd expect it to be a syntax error but that's not how Bash works, it just doesn't give the expected result.

Also compare without the variable assignment:

$ echo hello > somefile
$ `(cat somefile | head -1)` | tee -a dump.txt
bash: hello: command not found

The first line of somefile is not echoed to stdout, but interpreted as a command instead. Since the command can't be executed, tee doesn't get output, and isn't really executed either as there is no pipe to make.

0

You must log in to answer this question.

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