Duplication of file descriptors in redirection
Redirections are implemented via the dup family of system functions. dup
is short for duplication and when you do e.g.:
3>&2
you duplicate (dup2
) filedescritor 2 onto filedescriptor 3, possibly closing filedescriptor 3 if it's already open (which won't do a thing to your parent process, because this happens in a fork
ed off child (if it does not (redirections on shell functions in certain contexts), the shell will make it look as if it did)).
When you do:
1<someFile
it'll open
someFile
on a new file descriptor (that's what the open
syscall normally does) and then it'll dup2
that filedescriptor onto 1.
What the manual says is that if one of the special dev files listed takes the place of someFile
, the shell will skip the open
-on-a-new-fd step and instead go directly to dup2
ing the matching filedescriptor (i.e., 1 for /dev/stdout, etc.) onto the target (filedescriptor on the left side of the redirection).
Related videos on Youtube
![Tim](https://i.stack.imgur.com/3PCjR.png?s=256&g=1)
Tim
Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.
Updated on September 18, 2022Comments
-
Tim almost 2 years
From the The GNU Bash Reference Manual, section 3.6 Redirections:
Bash handles several filenames specially when they are used in redirections, as described in the following table:
/dev/fd/fd
- If fd is a valid integer,
file descriptor fd is duplicated.
/dev/stdin
- File descriptor 0 is duplicated.
/dev/stdout
- File descriptor 1 is duplicated.
/dev/stderr
- File descriptor 2 is duplicated.
what does "duplicated" mean here? Can you give some examples?
(The above excerpt is from Edition 4.3, last updated 2 February 2014, of The GNU Bash Reference Manual, for
Bash
, Version 4.3.)