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.
| improve this question | | | | |
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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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