4 ms·
Thank you for taking the time to write this very detailed and lucid explanation.
by tuatoru 3y ago
Thank you for taking the time to write this very detailed and lucid explanation.
- jcrites 3y agoFor additional clarification, `echo` doesn’t read from stdin, so `… | echo xyz` doesn’t do what you probably assume. Try running `echo a | echo b` and you’ll see that only “b” is printed. That’s because `echo b` doesn’t read the “a” sent to it on stdin (and also doesn’t print it). If you want a program to read from stdin and write to stdout, you can use the `cat`, e.g. `echo a | cat` will print “a”. Lastly, be aware that `echo` is usually a shell builtin that functions like `print`. I’m not sure of all the ways that it might behave differently, but something to be aware of (that it’s not a child process like `cat`).
- dietrichepp 3y agoThe way that shell builtins behave differently here is that SIGPIPE can take out the whole shell on the left side when echo is built-in. When you /bin/echo red, then it's a subprocess, and its parent shell continues on, so you always get green somewhere in the output.