Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
Thanks @frostschutz! – Kent Pawar Apr 19 at 13:50

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.