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.
1 Answer 1
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 withbash
(specifically) actually use the device in/dev
or does it usebash
's own built-in/simulated device?2018年04月25日 10:10:02 +00:00Commented Apr 25, 2018 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– Stephen Kitt2018年04月25日 10:12:04 +00:00Commented Apr 25, 2018 at 10:12 -
I was just about to go look at the source code. Reading the manual is cheating ;-) Thanks.2018年04月25日 10:14:13 +00:00Commented Apr 25, 2018 at 10:14
-
1They'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 (tryecho 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 withcat
orcat -
for instance).Stéphane Chazelas– Stéphane Chazelas2018年04月25日 10:14:59 +00:00Commented Apr 25, 2018 at 10:14 -
1@Kusalananda, in the case of
cat /dev/stdin
, sincecat
is not a shell builtin inbash
,bash
cannot "emulate" anything. It just passes/dev/stdin
as argument tocat
. It's only in the target of redirections thatbash
may emulate those.Stéphane Chazelas– Stéphane Chazelas2018年04月25日 10:16:11 +00:00Commented Apr 25, 2018 at 10:16