28

I see that this has the behavior:

[root@divinity test]# echo 0 > file.txt
[root@divinity test]# cat file.txt
0
[root@divinity test]# echo 0> file.txt

[root@divinity test]# cat file.txt

I also noticed that if I include "" then it works as expected:

[root@divinity test]# echo 0""> file.txt
[root@divinity test]# cat file.txt
0

I imagine this is all just part of IO redirection, but I do not quite understand what echo 0> is doing.

5
  • @zevzek "nohup experts" -- assuming bash or a shell with equivalent extensions, why would any expert use nohup instead of explicit redirection and disown -h? (Honest question: I suspect I may learn something from your response). Oct 10, 2021 at 18:17
  • @Charles see this Q&A on nohup v. disown. Oct 10, 2021 at 18:45
  • @StephenKitt, ...hmm. That's comparing nohup against disown without redirection, whereas it's disown with redirection (for all three of the standard FDs) that I'd call a fair comparison. Thus, almost all of the time spent in the analysis above is about the results of the subprocesses still inherting stdin/stdout/stderr. Oct 10, 2021 at 18:51
  • @zevzek I sit corrected, thanks. Oct 11, 2021 at 13:02
  • @zevzek What does using a subshell have to do with not needing to redirect stdin from /dev/null?
    – jrw32982
    Oct 13, 2021 at 21:15

2 Answers 2

56

In echo 0 > file.txt, with the spaces, > file.txt causes the shell to redirect standard output so that it is written to file.txt (after truncating the file if it already exists). The rest of the command, echo 0, is run with the redirections in place.

When a redirection operator is prefixed with an unquoted number, with no separation, the redirection applies to the corresponding file descriptor instead of the default. 0 is the file descriptor for standard input, so 0> file.txt redirects standard input to file.txt. (1 is standard output, 2 is standard error.) The number is treated as part of the redirection operator, and no longer as part of the command’s arguments; all that’s left is echo.

You can see this happening more obviously if you include more content in the echo arguments:

$ echo number 0 > file.txt
$ echo number 0> file.txt
number

number shows up in the second case because standard output isn’t redirected.

This variant of the redirection operator is more commonly done with standard error, 2> file.txt.

Redirecting standard input in this way will break anything which actually tries to read from its standard input; if you really want to redirect standard input, while allowing writes, you can do so with the <> operator:

cat 0<> file.txt
1
  • 4
    I think this is a good answer (upvoted), but I think it will benefit from 1 or 2 sentences better explaining what adding the space after the number is doing. Oct 8, 2021 at 6:26
0

File descriptor zero is the input stream. 0>filename will place the input stream into filename. 0 > filename will put 0 into the file filename. Notice the spacing.

1
  • 9
    what does "will place the input stream into filename" mean, exactly?
    – ilkkachu
    Oct 8, 2021 at 14:07

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.