Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It's a question about bash shell command. "sh a.sh <&0 >&0" What dose it mean? Especially, I'm not very clear about what does &0 mean?

share|improve this question

migrated from serverfault.com Jul 27 '13 at 9:39

This question came from our site for professional system and network administrators.

2 Answers 2

& followed by a digit in that case indicates a file number. File number 0 is standard input (stdin). The < and > characters indicate redirection of the process's standard in and standard out respectively.

So, in short, this command means to run a.sh in sh (as a shell script), redirecting both the script's (really, the subshell's) standard in and out to standard in.

In this case, <&0 is redundant (this is the default behaviour). >&0 will mean it prints what would otherwise be printed on standard out to standard in instead, relative to the parent shell. Most ttys will echo such output, and so in consequence the redirection has almost no effect and probably no visible effect.

One case this would be reasonable is if the parent shell's standard output file were strangely closed. This is a strange edge case.

share|improve this answer

n>&p and n<&p are the same operator and are for duplicating the file descriptor (fd) p onto the file descriptor n. Or said otherwise, they redirect the file descriptor n to whatever resource fd p 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 as p. That is, if p was open for writing, so will be n even if the n<&p operator is used.

The only difference between the two operators is when n is not specified. >&p redirects stdout (is like 1>&p or 1<&p) and <&p redirects stdin (is like 0<&p or 0>&p).

So <&0 is like 0<&0, so redirects stdin to whatever resource stdin was redirected to, so does nothing useful, it's a no-op and doesn't make much sense.

>&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.

The only place where n>&n makes sense is with zsh and it's mult_IOs feature. In zsh:

some-cmd >&1 > some-file
# that is: some-cmd 1>&1 1> some-file

Redirects the standard output of some-cmd to both whatever stdout was before (&1) and some-file, as if you had written:

some-cmd | tee some-file

While

some-cmd <&0 < some-file
# that is: some-cmd 0<&0 0< some-file

would feed first the original stdin and then some-file as input to some-cmd as if you had written:

cat - some-file | some-cmd

But in cmd <&0 >&0, fd 0 is redirected only once, so that does not apply.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.