0

So as I'm writing my own shell for school, I have to implement redirections.

I've looked at the GNU Bash manual, and a fair amount of tutorials, but I'm still having troubles understanding what exactly means the ampersand in these redirections. I know when I have to use them when using simple redirections, like &>file or 2>&- but I don't get the precise meaning of it.

Could someone explain?

Zest
  • 3
  • 1
    That's just the character used to specify the action. What more are you looking for? Why not ask what does the > mean or what does | mean or why was $ chosen? Additionally &> is sort of a non-standard redirection. – jesse_b Feb 28 '20 at 14:32
  • @jesse_b it's the action that I don't understand. I'm gonna have a look at that thread, thanks. – Zest Feb 28 '20 at 14:34
  • Do you know what the rough meaning of it is? Do you know what stdout and stderr are? Is there something in specific that you're missing about the ampersand? – ilkkachu Feb 28 '20 at 14:54
  • 2
    Your mistake is to separate the & from the >. Just like in C or javascript the != operator is not ! and = but just !=, so it's >& or <& in the shell language. All those operators are explained in the bash manpage, including the fact that [n]>&- is identical to [n]<&-, and [n]>&n is identical to [n]<&n (where n is a number), but >& path is something completely different than >&n (where path is NOT a number). –  Feb 28 '20 at 15:16

1 Answers1

0

The ampersand in >& does not have a functional role. Its role is more like the role of the letter c in cat.

The notations >&, <& etc have the roles explained in the man page for bash.

You could regard the & as indicating that the redirection involves merging two IO streams. But it is more of part of a mnemonic spelling choice, not a separate, independent per-character action or role.

The meta character & also occurs in other contexts as & at end of line or in the && operator. But this is a bit like c appearing in both cat and cd. It is a common element in the spelling of several unrelated features.

  • Though the "merging" here is just the same as with > $path 2> $path, that is, a following redirection can still "unmerge" them – ilkkachu Feb 28 '20 at 16:46