In Bash, what is file descriptor 255 for, can I use it?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I understand file descriptor (or file handler) is an file IO technique in Linux systems.
I also know that each process has 3 standard streams (namely stdin, stdout and stderr) that are represented by files with descriptors from 0 to 3.
However, I notice that all the processes I examined with lsof -p <pid>
has an extra file descriptor 255
with read permission.
From this answer, I learned that this feature is specific to Bash shell, however both the answer the and the referenced source didn't really explain what this file descriptor is for.
My question:
- What is the 255 file descriptor for?
- Can I make use of it in my Bash script or is it just an internal working mechanism that is not supposed to be used/manipulated manually?
bash file-descriptors
 |Â
show 2 more comments
up vote
1
down vote
favorite
I understand file descriptor (or file handler) is an file IO technique in Linux systems.
I also know that each process has 3 standard streams (namely stdin, stdout and stderr) that are represented by files with descriptors from 0 to 3.
However, I notice that all the processes I examined with lsof -p <pid>
has an extra file descriptor 255
with read permission.
From this answer, I learned that this feature is specific to Bash shell, however both the answer the and the referenced source didn't really explain what this file descriptor is for.
My question:
- What is the 255 file descriptor for?
- Can I make use of it in my Bash script or is it just an internal working mechanism that is not supposed to be used/manipulated manually?
bash file-descriptors
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
1
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I understand file descriptor (or file handler) is an file IO technique in Linux systems.
I also know that each process has 3 standard streams (namely stdin, stdout and stderr) that are represented by files with descriptors from 0 to 3.
However, I notice that all the processes I examined with lsof -p <pid>
has an extra file descriptor 255
with read permission.
From this answer, I learned that this feature is specific to Bash shell, however both the answer the and the referenced source didn't really explain what this file descriptor is for.
My question:
- What is the 255 file descriptor for?
- Can I make use of it in my Bash script or is it just an internal working mechanism that is not supposed to be used/manipulated manually?
bash file-descriptors
I understand file descriptor (or file handler) is an file IO technique in Linux systems.
I also know that each process has 3 standard streams (namely stdin, stdout and stderr) that are represented by files with descriptors from 0 to 3.
However, I notice that all the processes I examined with lsof -p <pid>
has an extra file descriptor 255
with read permission.
From this answer, I learned that this feature is specific to Bash shell, however both the answer the and the referenced source didn't really explain what this file descriptor is for.
My question:
- What is the 255 file descriptor for?
- Can I make use of it in my Bash script or is it just an internal working mechanism that is not supposed to be used/manipulated manually?
bash file-descriptors
bash file-descriptors
asked 4 hours ago


Tran Triet
878
878
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
1
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago
 |Â
show 2 more comments
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
1
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
1
1
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
For the last part of your question:
can I use it?
From man bash
:
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
So, if you mean use as creating a new fd with that number the answer is no.
If you mean use as: "write to that fd":
$ echo hello >/dev/fd/255"
Or to read from it:
$ read a </dev/fd/255
abc
$ echo "$a"
abc
the answer is yes.
what is file descriptor 255 for?
To keep a copy of fd 1 (/dev/stdout
) and fd 0 (/dev/stdin
) in case those get blocked.
More detail.
Other shells may use a different number (like 10 in zsh)
$ zsh
mail% ls -l /proc/self/fd /proc/$$/fd/* &
[1] 3345
mail% lrwx------ 1 isaac isaac 64 Oct 14 09:46 /proc/3250/fd/0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/10 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/2 -> /dev/pts/2
/proc/self/fd:
total 0
lrwx------ 1 isaac isaac 64 Oct 14 09:50 0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 2 -> /dev/pts/2
lr-x------ 1 isaac isaac 64 Oct 14 09:50 3 -> /proc/3345/fd
[1] + done ls -l /proc/self/fd /proc/$$/fd/*
mail%
From mail list:
When executing a 'script' bash sets its private number of FDs to 255.
It then opens a script file, duplicating the fd to its max FD -1 so
that when it ultimately closes that FD it doesn't close the original
input/output FDs, leaving the shell with no interactive I/O.
Line 692 of shell.c in my version of bash.
Cheers,
Dick Johnson
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
For the last part of your question:
can I use it?
From man bash
:
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
So, if you mean use as creating a new fd with that number the answer is no.
If you mean use as: "write to that fd":
$ echo hello >/dev/fd/255"
Or to read from it:
$ read a </dev/fd/255
abc
$ echo "$a"
abc
the answer is yes.
what is file descriptor 255 for?
To keep a copy of fd 1 (/dev/stdout
) and fd 0 (/dev/stdin
) in case those get blocked.
More detail.
Other shells may use a different number (like 10 in zsh)
$ zsh
mail% ls -l /proc/self/fd /proc/$$/fd/* &
[1] 3345
mail% lrwx------ 1 isaac isaac 64 Oct 14 09:46 /proc/3250/fd/0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/10 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/2 -> /dev/pts/2
/proc/self/fd:
total 0
lrwx------ 1 isaac isaac 64 Oct 14 09:50 0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 2 -> /dev/pts/2
lr-x------ 1 isaac isaac 64 Oct 14 09:50 3 -> /proc/3345/fd
[1] + done ls -l /proc/self/fd /proc/$$/fd/*
mail%
From mail list:
When executing a 'script' bash sets its private number of FDs to 255.
It then opens a script file, duplicating the fd to its max FD -1 so
that when it ultimately closes that FD it doesn't close the original
input/output FDs, leaving the shell with no interactive I/O.
Line 692 of shell.c in my version of bash.
Cheers,
Dick Johnson
add a comment |Â
up vote
3
down vote
For the last part of your question:
can I use it?
From man bash
:
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
So, if you mean use as creating a new fd with that number the answer is no.
If you mean use as: "write to that fd":
$ echo hello >/dev/fd/255"
Or to read from it:
$ read a </dev/fd/255
abc
$ echo "$a"
abc
the answer is yes.
what is file descriptor 255 for?
To keep a copy of fd 1 (/dev/stdout
) and fd 0 (/dev/stdin
) in case those get blocked.
More detail.
Other shells may use a different number (like 10 in zsh)
$ zsh
mail% ls -l /proc/self/fd /proc/$$/fd/* &
[1] 3345
mail% lrwx------ 1 isaac isaac 64 Oct 14 09:46 /proc/3250/fd/0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/10 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/2 -> /dev/pts/2
/proc/self/fd:
total 0
lrwx------ 1 isaac isaac 64 Oct 14 09:50 0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 2 -> /dev/pts/2
lr-x------ 1 isaac isaac 64 Oct 14 09:50 3 -> /proc/3345/fd
[1] + done ls -l /proc/self/fd /proc/$$/fd/*
mail%
From mail list:
When executing a 'script' bash sets its private number of FDs to 255.
It then opens a script file, duplicating the fd to its max FD -1 so
that when it ultimately closes that FD it doesn't close the original
input/output FDs, leaving the shell with no interactive I/O.
Line 692 of shell.c in my version of bash.
Cheers,
Dick Johnson
add a comment |Â
up vote
3
down vote
up vote
3
down vote
For the last part of your question:
can I use it?
From man bash
:
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
So, if you mean use as creating a new fd with that number the answer is no.
If you mean use as: "write to that fd":
$ echo hello >/dev/fd/255"
Or to read from it:
$ read a </dev/fd/255
abc
$ echo "$a"
abc
the answer is yes.
what is file descriptor 255 for?
To keep a copy of fd 1 (/dev/stdout
) and fd 0 (/dev/stdin
) in case those get blocked.
More detail.
Other shells may use a different number (like 10 in zsh)
$ zsh
mail% ls -l /proc/self/fd /proc/$$/fd/* &
[1] 3345
mail% lrwx------ 1 isaac isaac 64 Oct 14 09:46 /proc/3250/fd/0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/10 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/2 -> /dev/pts/2
/proc/self/fd:
total 0
lrwx------ 1 isaac isaac 64 Oct 14 09:50 0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 2 -> /dev/pts/2
lr-x------ 1 isaac isaac 64 Oct 14 09:50 3 -> /proc/3345/fd
[1] + done ls -l /proc/self/fd /proc/$$/fd/*
mail%
From mail list:
When executing a 'script' bash sets its private number of FDs to 255.
It then opens a script file, duplicating the fd to its max FD -1 so
that when it ultimately closes that FD it doesn't close the original
input/output FDs, leaving the shell with no interactive I/O.
Line 692 of shell.c in my version of bash.
Cheers,
Dick Johnson
For the last part of your question:
can I use it?
From man bash
:
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
So, if you mean use as creating a new fd with that number the answer is no.
If you mean use as: "write to that fd":
$ echo hello >/dev/fd/255"
Or to read from it:
$ read a </dev/fd/255
abc
$ echo "$a"
abc
the answer is yes.
what is file descriptor 255 for?
To keep a copy of fd 1 (/dev/stdout
) and fd 0 (/dev/stdin
) in case those get blocked.
More detail.
Other shells may use a different number (like 10 in zsh)
$ zsh
mail% ls -l /proc/self/fd /proc/$$/fd/* &
[1] 3345
mail% lrwx------ 1 isaac isaac 64 Oct 14 09:46 /proc/3250/fd/0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/10 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 /proc/3250/fd/2 -> /dev/pts/2
/proc/self/fd:
total 0
lrwx------ 1 isaac isaac 64 Oct 14 09:50 0 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 1 -> /dev/pts/2
lrwx------ 1 isaac isaac 64 Oct 14 09:50 2 -> /dev/pts/2
lr-x------ 1 isaac isaac 64 Oct 14 09:50 3 -> /proc/3345/fd
[1] + done ls -l /proc/self/fd /proc/$$/fd/*
mail%
From mail list:
When executing a 'script' bash sets its private number of FDs to 255.
It then opens a script file, duplicating the fd to its max FD -1 so
that when it ultimately closes that FD it doesn't close the original
input/output FDs, leaving the shell with no interactive I/O.
Line 692 of shell.c in my version of bash.
Cheers,
Dick Johnson
edited 25 mins ago
answered 40 mins ago


Isaac
8,20011139
8,20011139
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475389%2fin-bash-what-is-file-descriptor-255-for-can-i-use-it%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
In my opinion your questions were answered on the linked page.
– Cyrus
3 hours ago
I will examine the answer again to see if it answers my question. I only noticed now that you are the one who gave that answer ;)
– Tran Triet
3 hours ago
1
@Cyrus saying that "it's a little trick" without explaining what that "little trick" is is not a proper answer.
– mosvy
3 hours ago
The first comment on the linked answer seems to have a better discussion... The last reply is probably what you are looking for...
– RubberStamp
2 hours ago
Related: askubuntu.com/a/866722/772601
– Isaac
58 mins ago