sh a.sh <&0 >&0
— what does it mean?
In particular, I'm not very clear about what &0
means.
Shell – What does “sh a.sh <&0 >&0” mean
io-redirectionshell
io-redirectionshell
sh a.sh <&0 >&0
— what does it mean?
In particular, I'm not very clear about what &0
means.
Best Answer
n>&p
andn<&p
are the same operator and are for duplicating the file descriptor (fd)p
onto the file descriptorn
. Or said otherwise, they redirect the file descriptorn
to whatever resource fdp
is redirected to.The
<
and>
are not used to determine what direction (reading or writing) the redirected file descriptor will be used.n
will get the same direction asp
. That is, ifp
was open for writing, so will ben
even if then<&p
operator is used.The only difference between the two operators is when
n
is not specified.>&p
redirects stdout (is like1>&p
or1<&p
) and<&p
redirects stdin (is like0<&p
or0>&p
).So
<&0
is like0<&0
, so redirects stdin to whatever resource stdin was redirected to, so does nothing useful, it's usually a no-op and doesn't make much sense.But not always a no-op, not in every shell. When job control is disabled, POSIX requires stdin of command in the background to be redirected to
/dev/null
or an equivalent file. In Bash (tested in 4.4.12)<&0
overrides this. Compare(ls -l /proc/self/fd/0 &)
and(<&0 ls -l /proc/self/fd/0 &)
. In some cases this is useful.>&0
duplicates the fd 0 onto the fd 1. Because the fd 1 (stdout), is by convention only used for writing, that>&0
only makes sense if fd 0 was open in read+write mode.That would be the case in cases where fd 0 points to the terminal device, because terminal emulators or
getty
would generally open the terminal device in read+write mode and assign fds 0, 1 and 2 to it.So maybe whoever wrote that wanted to redirect stdout to the terminal assuming that stdin was pointing to it.
One place where
n>&n
makes sense is with zsh and itsmult_IOs
feature. Inzsh
:Redirects the standard output of
some-cmd
to both whatever stdout was before (&1) andsome-file
, as if you had written:While
would feed first the original stdin and then
some-file
as input tosome-cmd
as if you had written:But in
cmd <&0 >&0
, fd 0 is redirected only once, so that does not apply.n>&n
can also have an interesting side effect in some shells (ksh
,zsh
, notdash
,bash
noryash
) in that it triggers an error and gives up running the command if the file descriptorn
is not open. So, in those shells,would avoid running
cmd
in the pathological condition wherestdin
is closed: