What is the file descriptor 3 assigned by default?

$ ls -og /proc/self /proc/self/fd
lrwxrwxrwx 1 64 Jun 18 11:12 /proc/self -> 32157

/proc/self/fd:
total 0
lrwx------ 1 64 Jun 22  2012 0 -> /dev/tty1
lrwx------ 1 64 Jun 22  2012 1 -> /dev/tty1
lrwx------ 1 64 Jun 22  2012 2 -> /dev/tty1
lr-x------ 1 64 Jun 22  2012 3 -> /proc/32157/fd

What is the file descriptor 3 assigned by default?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Nothing: there are three standard file descriptions, STDIN, STDOUT, and STDERR. They are assigned to 0, 1, and 2 respectively.

What you are seeing there is an artifact of the way ls(1) works: in order to read the content of the /proc/self/fd directory and display it, it needs to open that directory.

That means it gets a file handle, typically the first available … thus, 3.

If you were to run, say, cat on a separate console and inspect /proc/${pid}/fd for it you would find that only the first three were assigned.

Method 2

This can also be used to read pipelines of running processes.

$ head /proc/21028/fd/3
declare -ax a_name='([0]="
8006333 (10.161.154.1)" [1]="

6006583 (10.179.231.1)" [2]="

9001437 (10.125.167.1)" [3]="

2003192 (10.138.247.1)" [4]="

4005015 (10.120.139.1)" [5]="


$ grep -n printf pol-grab.sh  | grep ^54
54:            export add_the_name="$(printf "%sn" "$(declare -p a_name)")"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x