I've found mostly confusing the syntax about closing file descriptors. Taking as example: n>&-, i'm closing a file descriptor called n, i'm right ?

But i knew that if i want redirect something into another file descriptor, i must do that this way n>&1, so the & is used as marker for file descriptors, only when >, or others redirection commands are used, and also this should be correct, right ?

Futhermore, - symbol can be used for redirection from/to stdin or stdout as into: cat -

So taking into account the rules said above, why n>&- should close a file descriptor ? It seems like a redirection to stdin...

PS: For references i've used http://tldp.org/LDP/abs/html/io-redirection.html and http://tldp.org/LDP/abs/html/special-chars.html

1 Answer 1


There are rules, and there are exceptions.

man bash | grep -C 3 '&-'
       Each redirection that may be preceded by a file descriptor  number  may
       instead be preceded by a word of the form {varname}.  In this case, for
       each redirection operator except >&- and <&-, the shell will allocate a
       file  descriptor  greater than or equal to 10 and assign it to varname.
       If >&- or <&- is preceded by {varname}, the value  of  varname  defines
       the file descriptor to close.
  • Thank you ! I've forgot to check man, i've trusted too much my references without seeking anything else.
    – Luca
    Commented Mar 2, 2017 at 23:07

You must log in to answer this question.

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