When you run without the pipe "|" symbol
samtools view file.bam 1>/dev/null
1 is interpreted as stdout so it is exactly the command we all are very used to:
samtools view file.bam > /dev/null
You know it. This is a way to read bam file without header into a SAM file if you replace "/dev/null" with "your.SAM". It takes very long time. /dev/null just discards all the data that was put into it, put it actualy first has to get it from the samtools. The whole file is read despite this /dev/null anyway.
When you add the "|" pipe symbol the way you mentioned:
samtools view file.bam | 1>/dev/null
Your first command tries to read the whole bam file without the header and pipe it (kind of sam file) via stdout into a second command, in your case it is "1". Most likely you do not have program or script called "1" in your system. So the second command will end immediately with an error and as it is the last command in your pipe before redirect it will terminate the whole pipe. What you see on your screen is what the first command had time to write as an error message into stderr since stdout was redirected with pipe. When bam file is truncated stderr contains information that file was truncated when it is a valid bam file stderr is empty so no message. You can test all this by running this:
samtools view file.bam | biostars
since you most likely do not have a program called "biostars" in your system it will behave almost as your "samtools view file.bam | 1>/dev/null" other than it will clearly tell you that there was no "biostars" command found. Then you can run command
samtools view file.bam 2 > err.txt
terminate it immediately with Control+C since it will try to print the whole sam file into your stdout. Now take a look at the contents of the err.txt. It will be empty for proper bam and will have an error message for truncated one. This means that samtools already raised an error before you terminated it manually a moment after it started to read a bam file. So if you do
samtools view file.bam 2 > err.txt | wow
you will get information whether bam is normal or truncated in err.txt file if you do not have program called "wow" in your system =)
Actually not: only if a space is inserted (unlike in the original example),
1
will be taken as a command name.This is an interesting point. The output is different in apearance, but my understanding was that metacharacters | > space tab < ; & ( ) are all parsed by bash to separate words so 1 will be treated as a command in both cases. Probably we need a person that better understands bash to explain why stderr is printed in one situation but not the other and if 1 is not a command if there is no space between 1 and >.
That would be a good question for UNIX StackExchange. I tried
Nowhere do I see an error, so I'm sure
1>
is not being taken as a command anywhere.It's because the stuff after the | isn't being fed to a command, or rather, it's being fed to a null command, the output of which (which is nothing) is sent to tmp. So the 1>tmp bit is working exactly as you expect, it's just that there's an invisible null command between the | and the 1. "true" and "false" are also both null commands, so you can replicate with them:
The command being executed is equivalent to
:
, as I understand. Bash continues to amaze me.http://stackoverflow.com/questions/43948424/piping-to-a-process-when-the-process-doesnt-exist