3

From https://www.gnu.org/s/bash/manual/html_node/Redirections.html

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection allows commands’ file handles to be duplicated, opened, closed, made to refer to different files, and can change the files the command reads from and writes to. Redirection may also be used to modify file handles in the current shell execution environment.

Yet, the following text in the link shows that redirections operate on file descriptors (which are integers).

File handles and file descriptors are different. From https://en.wikipedia.org/wiki/File_descriptor#Overview, file handles are data structures of FILE in the C standard library. File descriptors are objects in Unix and Unix-like operating systems.

What does the bash manual mean by "file handles" in the quote?

3

The Bash documentation is using the term 'file handle' as a synonym for 'file descriptor'.

There's no requirement for programs to use the C Standard Library for I/O. Obviously, if they do, they can use fdopen() to obtain a (pointer to a) FILE structure from one of the file descriptors.

  • Thanks. Why does the following text in the link says that redirections operate on file descriptors (instead of file handles)? – Tim Dec 11 '15 at 0:09
  • @Tim - no idea; perhaps (speculation) they were written by different people (by which I include the same person at different times - deep philisophical question, there!) – Toby Speight Dec 11 '15 at 4:54
2

(In a Linux system) If you write:

ls -l /proc/$$/fd

It will write the list of fd (file descriptors) open:

lrwx------ 1 user user 64 dic  8 00:06 0 -> /dev/pts/12
lrwx------ 1 user user 64 dic  8 00:06 1 -> /dev/pts/12
lrwx------ 1 user user 64 dic  8 00:06 2 -> /dev/pts/12
lrwx------ 1 user user 64 dic  8 00:06 255 -> /dev/pts/12

Those are also called "file handles".

As you can see, the numbers are 0,1,2. Those are the same as stdin(0), stdout(1), stderr(2).

So, the redirection work with those numbers (as >&2) and some new numbers could be created (as >&18).

Those file descriptors could also be accessed by some other older names, like: /dev/stdout, /dev/fd1, or some others.

Wikipedia has a reasonable good description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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