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
nohup
instead of explicit redirection anddisown -h
? (Honest question: I suspect I may learn something from your response). – Charles Duffy Oct 10 '21 at 18:17nohup
v.disown
. – Stephen Kitt Oct 10 '21 at 18:45nohup
againstdisown
without redirection, whereas it'sdisown
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. – Charles Duffy Oct 10 '21 at 18:51/dev/null
? – jrw32982 Oct 13 '21 at 21:15