In Bash, what is file descriptor 255 for, can I use it?

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



  1. What is the 255 file descriptor for?

  2. 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?









share|improve this question





















  • 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














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:



  1. What is the 255 file descriptor for?

  2. 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?









share|improve this question





















  • 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












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:



  1. What is the 255 file descriptor for?

  2. 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?









share|improve this question













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:



  1. What is the 255 file descriptor for?

  2. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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










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







share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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







    share|improve this answer


























      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







      share|improve this answer
























        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







        share|improve this answer














        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








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 25 mins ago

























        answered 40 mins ago









        Isaac

        8,20011139




        8,20011139



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery