0

Sometimes, in shell scripts, I see like

echo yes 1>&5

I understand it redirect to somewhere, but I would like to know more detail.

And more specifically, what do 1, >, &, 5 mean?

I would be appreciated if you explain them token by token.

Or any pointer that explains.

1
  • The title says 1&>5, but the body of your post says 1>&5. Edit the message and fix it.
    – Lucio
    Aug 15, 2012 at 2:19

1 Answer 1

3

While searching in the internet a bit, i found this.

n>&m    means redirect FD n to the same places as FD m. Eg, 2>&1 means send STDERR to the same place that STDOUT is going to.

STDIN is FD 0, while STDOUT is FD 1, and STDERR is FD 2

ref: http://www.linuxsa.org.au/tips/io-redirection.html

File descriptor 5 is sometimes used because it is the lowest numbered file descriptor that was never really used for anything else (FD 4 was /dev/tty on some very old unixes and FD 3 has had various, non-standard, system specific, uses in another Unix). Although since neither 3 or 4 have been used in over 2 decades it is probably just coincidence (or tradition) that you have noticed FD 5 popping up.

ref: http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/File-Descriptors.html

As to the reason it is used. By using a file descriptor the script-writer avoids the need to actually write to a file, which could save him time and make the script cleaner (no possibility of accidently leaving around files). Furthermore it lets you do all the cool things with redirecting streams to different locations and so on.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .