1

Several resources state the file descriptor 0 is connected to the keyboard.

0_kb

I have learned that echo does not receive input from stdin (or 0).

This feels contradictory to me. When I run echo foo I'm using the keyboard as a way to pass input to the command. In what sense is stdin connected to they keyboard that makes it so that there isn't a contradiction here?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Redirect
  • 107
  • The descriptions of all three aren't true in general. E.g.they could also refer to files or the input/outputs of other programs – Torin May 02 '19 at 12:09
  • When you're typing echo ... that isn't done by echo itself, it's done by the shell, which in this case is getting input from the user's keyboard – Torin May 02 '19 at 12:12
  • 1
    Believing that stdin is connected to a keyboard is the biggest error beginners make, and it gets ingrained in their thinking. It is wrong, Often, a keyboard is associated with the stdin of a program but more often it is not. stdin may be a regular file on which you can seek. "Input from a keyboard" is sometimes a useful way to think about it, but do not make the mistake of thinking it is always true. – William Pursell May 02 '19 at 12:32
  • @Torin If I understand things correctly, by default those things are true in general, except for they keyboard. Only after purposeful modification do they not hold. – Redirect May 02 '19 at 12:53
  • @Redirect There's no "by default" here. If a daemon starts a process, it will "by default" use the same file descriptors as the daemon itself, which may or may not be set up. If they are they are likely directed to either log files or /dev/null. The above descriptions only really come true if the process has a controlling tty and said tty is the stdin, stdout and stderr. – Torin May 02 '19 at 13:11

1 Answers1

3

First, it is not always true that stdin is associated with a keyboard. Second, you are not using stdin to pass anything to echo. You are using your keyboard to pass the string echo foo to your shell. The shell parses that string and invokes a command called echo (whether the command is a builtin to the shell or the executable /usr/bin/echo or /bin/echo is irrelevant) which parses its arguments, ignores its stdin, and writes out the string foo\n

  • Thanks, this is much closer to how I assumed it should be. Just to make it crystal clear, the statement "stdin is connect to the keyboard" is false, correct? I have a follow up question. Itś not that echo does not receive input from stdin as I said my questin, but rather then it ignores it, right? – Redirect May 02 '19 at 12:52
  • The statement is not necessarily false, but it is not always true. And, yes, echo ignores its stdin. – William Pursell May 02 '19 at 12:54
  • I still don't get it after all. There is a default behavior, right? The default behavior is supposedly the one in the table in my question. I'm challenging/trying to understand the connection to the keyboard. What is this connection more precisely? As you explained, the shell parses a string. So it seems to me that the connection is made to an output of what the shell does to this string. – Redirect May 02 '19 at 13:00
  • If you invoke a simple command from an interactive shell with no redirects, then that command's stdin will be associated with the keyboard (with a lot extraneous detail omitted, eg if you are in a graphical environment and you move the focus out of the window, etc). s – William Pursell May 02 '19 at 13:24
  • So when you type echo foo in a shell, echo does have its stdin associated with your keyboard, but it ignores it. – William Pursell May 02 '19 at 13:25
  • The default behavior is that a process inherits its stdin from its parent. In the case of a shell, the shell spawns a command and the program inherits its stdin from the shell, and in the case of an interactive shell that is usually associated with a keyboard. The reason I point out that it is a mistake to think that stdin is always a keyboard is that the vast majority of processes running in a unix/linux environment are not spawned from an interactive shell. And even in an interactive shell, a large percentage of processes are redirected or in pipelines. – William Pursell May 02 '19 at 13:35