0

In the third edition of "Learning the Bash shell" by Newham and Rosenblatt it is written in page 15 as a comment:

if a particular UNIX utility doesn't accept standard input when you leave out the filename argument, try using a dash (0) as the argument. Some NIX systems provide standard input as a file so you could try providing the file /dev/stdin as the input file argumnet.

The last sentence isn't clear to me - what's the meaning of "provide standard input as a file"?

Isn't it dangerous to "repeatedly" create files named /dev/stdin in a system (I personally use Ubuntu 16.04).

BTW I came across a similar case in this post.

6

This isn’t about creating /dev/stdin, it’s about using it: you specify /dev/stdin as the file to be used with whatever command doesn’t support standard input by default (or using -), to make it use its standard input anyway.

Thus

cat
cat -
cat /dev/stdin

all make cat read from its standard input (albeit in a slightly different way when using /dev/stdin, with a different file descriptor, which means it’s not equivalent in all cases). I’m not creating /dev/stdin, I’m using the pre-existing device node as an argument to cat.

  • Does accessing /dev/stdin like this with bash (specifically) actually use the device in /dev or does it use bash's own built-in/simulated device? – Kusalananda Apr 25 '18 at 10:10
  • 2
    @Kusalananda the Bash manual says “If the operating system on which Bash is running provides these special files, bash will use them; otherwise it will emulate them internally with the behavior described below.” so if there is a node in /dev Bash uses that. – Stephen Kitt Apr 25 '18 at 10:12
  • I was just about to go look at the source code. Reading the manual is cheating ;-) Thanks. – Kusalananda Apr 25 '18 at 10:14
  • 1
    They're all equivalent except on Linux (and possibly Cygwin), where /dev/stdin is a link to the file opened on stdin. Opening it will not give you the same open file description as on stdin (contrary to on other systems). On Linux, opening /dev/stdin won't work when stdin is a socket (try echo foo | cat /dev/stdin in ksh93 for instance), could give you the other end of a pipe. For seekable files (like regular files), it would open the file at the beginning (try (head > /dev/null; cat /dev/stdin) < /etc/passwd and compare with cat or cat - for instance). – Stéphane Chazelas Apr 25 '18 at 10:14
  • 1
    @Kusalananda, in the case of cat /dev/stdin, since cat is not a shell builtin in bash, bash cannot "emulate" anything. It just passes /dev/stdin as argument to cat. It's only in the target of redirections that bash may emulate those. – Stéphane Chazelas Apr 25 '18 at 10:16

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.