Segmentation fault output is suppressed when piping stdin into a function. Why? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsDoes `Segmentation fault` message come under STDERR?Segmentation fault with dialogPiping commands, modify stdin write to stdoutPipe Fail (141) when piping output into tee — why?Segmentation fault when calling a recursive bash functionWhat exactly is the function piping into the other function in this fork bomb :() :;:?Why script that kill itself using a signal handler produce segmentation fault?Segmentation fault: 11 encounter while installing a programPiping PID into jstackWhy isn't it possible to read from `stdin` with `read` when piping a script to bash?

Did the new image of black hole confirm the general theory of relativity?

How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Hopping to infinity along a string of digits

How does ice melt when immersed in water

Do working physicists consider Newtonian mechanics to be "falsified"?

What do you call a plan that's an alternative plan in case your initial plan fails?

Is this wall load bearing? Blueprints and photos attached

Why is superheterodyning better than direct conversion?

How to test the equality of two Pearson correlation coefficients computed from the same sample?

Is a pteranodon too powerful as a beast companion for a beast master?

Change bounding box of math glyphs in LuaTeX

Why can't wing-mounted spoilers be used to steepen approaches?

First use of “packing” as in carrying a gun

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

Road tyres vs "Street" tyres for charity ride on MTB Tandem

Didn't get enough time to take a Coding Test - what to do now?

Can the prologue be the backstory of your main character?

Why is the object placed in the middle of the sentence here?

Scientific Reports - Significant Figures

Can a novice safely splice in wire to lengthen 5V charging cable?

When did F become S in typeography, and why?

Would it be possible to rearrange a dragon's flight muscle to somewhat circumvent the square-cube law?

Difference between "generating set" and free product?



Segmentation fault output is suppressed when piping stdin into a function. Why?



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsDoes `Segmentation fault` message come under STDERR?Segmentation fault with dialogPiping commands, modify stdin write to stdoutPipe Fail (141) when piping output into tee — why?Segmentation fault when calling a recursive bash functionWhat exactly is the function piping into the other function in this fork bomb :(): & ;:?Why script that kill itself using a signal handler produce segmentation fault?Segmentation fault: 11 encounter while installing a programPiping PID into jstackWhy isn't it possible to read from `stdin` with `read` when piping a script to bash?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















Let's define a function to execute a binary:



function execute() ./binary; 


Then define a second function to pipe a text file into the first function:



function test() execute; 


If binary crashes with a segfault, then calling test from the CLI will result in a 139 return code, but the error - "Segmentation fault" - will not be printed to the terminal.



"Segmentation fault" does get printed if we define test to call binary directly:



function test() ./binary; 


It also gets printed if we define to call execute without piping stdin into it:



function test() execute; 


Finally, it also gets printed if we redirect in.txt into execute directly instead of through a pipe:



function test() execute <in.txt; 


This was tested on Bash 4.4. Why is that?










share|improve this question



















  • 3





    FWIW, I can confirm the same behaviour with Bash 5.0.3.

    – Kusalananda
    2 hours ago







  • 1





    Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

    – Kusalananda
    2 hours ago

















4















Let's define a function to execute a binary:



function execute() ./binary; 


Then define a second function to pipe a text file into the first function:



function test() execute; 


If binary crashes with a segfault, then calling test from the CLI will result in a 139 return code, but the error - "Segmentation fault" - will not be printed to the terminal.



"Segmentation fault" does get printed if we define test to call binary directly:



function test() ./binary; 


It also gets printed if we define to call execute without piping stdin into it:



function test() execute; 


Finally, it also gets printed if we redirect in.txt into execute directly instead of through a pipe:



function test() execute <in.txt; 


This was tested on Bash 4.4. Why is that?










share|improve this question



















  • 3





    FWIW, I can confirm the same behaviour with Bash 5.0.3.

    – Kusalananda
    2 hours ago







  • 1





    Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

    – Kusalananda
    2 hours ago













4












4








4








Let's define a function to execute a binary:



function execute() ./binary; 


Then define a second function to pipe a text file into the first function:



function test() execute; 


If binary crashes with a segfault, then calling test from the CLI will result in a 139 return code, but the error - "Segmentation fault" - will not be printed to the terminal.



"Segmentation fault" does get printed if we define test to call binary directly:



function test() ./binary; 


It also gets printed if we define to call execute without piping stdin into it:



function test() execute; 


Finally, it also gets printed if we redirect in.txt into execute directly instead of through a pipe:



function test() execute <in.txt; 


This was tested on Bash 4.4. Why is that?










share|improve this question
















Let's define a function to execute a binary:



function execute() ./binary; 


Then define a second function to pipe a text file into the first function:



function test() execute; 


If binary crashes with a segfault, then calling test from the CLI will result in a 139 return code, but the error - "Segmentation fault" - will not be printed to the terminal.



"Segmentation fault" does get printed if we define test to call binary directly:



function test() ./binary; 


It also gets printed if we define to call execute without piping stdin into it:



function test() execute; 


Finally, it also gets printed if we redirect in.txt into execute directly instead of through a pipe:



function test() execute <in.txt; 


This was tested on Bash 4.4. Why is that?







bash command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







Dun Peal

















asked 2 hours ago









Dun PealDun Peal

1545




1545







  • 3





    FWIW, I can confirm the same behaviour with Bash 5.0.3.

    – Kusalananda
    2 hours ago







  • 1





    Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

    – Kusalananda
    2 hours ago












  • 3





    FWIW, I can confirm the same behaviour with Bash 5.0.3.

    – Kusalananda
    2 hours ago







  • 1





    Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

    – Kusalananda
    2 hours ago







3




3





FWIW, I can confirm the same behaviour with Bash 5.0.3.

– Kusalananda
2 hours ago






FWIW, I can confirm the same behaviour with Bash 5.0.3.

– Kusalananda
2 hours ago





1




1





Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

– Kusalananda
2 hours ago





Upon further investigation, this seems to be related to whether the shell is running in interactive or non-interactive mode. The error is printed in non-interactive mode.

– Kusalananda
2 hours ago










1 Answer
1






active

oldest

votes


















3














This diagnostic message is generated by the interactive shell's job control system, for the benefit of the user - it's not from the underlying program that crashed. When you pipe into a shell function a subshell is spawned to run the function, and this subshell is not treated as user-facing. If you call the function normally, it runs within the original shell, and the message is printed.



You can test this out by disabling job control in your current shell



set +m


and then running ./binary again: now it won't print anything there either. Re-enable job control with set -m.



Even a bare subshell has the same effect:



( : ; ./binary )


will print no diagnostic (two commands are required in there to avoid a subshell-eliding optimisation). Piping out of the function does it too.



Job control is disabled in the subshell, and even with it enabled manually, it's silenced. This is an unfortunate gap in the system. In a non-interactive shell the message would always be reported through a different mechanism, and anywhere else in an interactive shell it would as well.




If printing the diagnostic is important to you, making a script instead of a function will allow you to make sure it's always included. Since you're using the function in a pipeline, you can't do anything that requires a function anyway, so there's not a major cost to doing so.




I wouldn't go quite as far as to say this is a bug. One possible reason to behave in this way is to make command substitution $(...), which also runs a subshell, behave appropriately:



foo=$(echo|test)


shouldn't result in the diagnostic message being stored in foo, so that pipeline failures result in empty expansions. Another is as a way to temporarily suppress the diagnostic messages deliberately.






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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f512321%2fsegmentation-fault-output-is-suppressed-when-piping-stdin-into-a-function-why%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    This diagnostic message is generated by the interactive shell's job control system, for the benefit of the user - it's not from the underlying program that crashed. When you pipe into a shell function a subshell is spawned to run the function, and this subshell is not treated as user-facing. If you call the function normally, it runs within the original shell, and the message is printed.



    You can test this out by disabling job control in your current shell



    set +m


    and then running ./binary again: now it won't print anything there either. Re-enable job control with set -m.



    Even a bare subshell has the same effect:



    ( : ; ./binary )


    will print no diagnostic (two commands are required in there to avoid a subshell-eliding optimisation). Piping out of the function does it too.



    Job control is disabled in the subshell, and even with it enabled manually, it's silenced. This is an unfortunate gap in the system. In a non-interactive shell the message would always be reported through a different mechanism, and anywhere else in an interactive shell it would as well.




    If printing the diagnostic is important to you, making a script instead of a function will allow you to make sure it's always included. Since you're using the function in a pipeline, you can't do anything that requires a function anyway, so there's not a major cost to doing so.




    I wouldn't go quite as far as to say this is a bug. One possible reason to behave in this way is to make command substitution $(...), which also runs a subshell, behave appropriately:



    foo=$(echo|test)


    shouldn't result in the diagnostic message being stored in foo, so that pipeline failures result in empty expansions. Another is as a way to temporarily suppress the diagnostic messages deliberately.






    share|improve this answer



























      3














      This diagnostic message is generated by the interactive shell's job control system, for the benefit of the user - it's not from the underlying program that crashed. When you pipe into a shell function a subshell is spawned to run the function, and this subshell is not treated as user-facing. If you call the function normally, it runs within the original shell, and the message is printed.



      You can test this out by disabling job control in your current shell



      set +m


      and then running ./binary again: now it won't print anything there either. Re-enable job control with set -m.



      Even a bare subshell has the same effect:



      ( : ; ./binary )


      will print no diagnostic (two commands are required in there to avoid a subshell-eliding optimisation). Piping out of the function does it too.



      Job control is disabled in the subshell, and even with it enabled manually, it's silenced. This is an unfortunate gap in the system. In a non-interactive shell the message would always be reported through a different mechanism, and anywhere else in an interactive shell it would as well.




      If printing the diagnostic is important to you, making a script instead of a function will allow you to make sure it's always included. Since you're using the function in a pipeline, you can't do anything that requires a function anyway, so there's not a major cost to doing so.




      I wouldn't go quite as far as to say this is a bug. One possible reason to behave in this way is to make command substitution $(...), which also runs a subshell, behave appropriately:



      foo=$(echo|test)


      shouldn't result in the diagnostic message being stored in foo, so that pipeline failures result in empty expansions. Another is as a way to temporarily suppress the diagnostic messages deliberately.






      share|improve this answer

























        3












        3








        3







        This diagnostic message is generated by the interactive shell's job control system, for the benefit of the user - it's not from the underlying program that crashed. When you pipe into a shell function a subshell is spawned to run the function, and this subshell is not treated as user-facing. If you call the function normally, it runs within the original shell, and the message is printed.



        You can test this out by disabling job control in your current shell



        set +m


        and then running ./binary again: now it won't print anything there either. Re-enable job control with set -m.



        Even a bare subshell has the same effect:



        ( : ; ./binary )


        will print no diagnostic (two commands are required in there to avoid a subshell-eliding optimisation). Piping out of the function does it too.



        Job control is disabled in the subshell, and even with it enabled manually, it's silenced. This is an unfortunate gap in the system. In a non-interactive shell the message would always be reported through a different mechanism, and anywhere else in an interactive shell it would as well.




        If printing the diagnostic is important to you, making a script instead of a function will allow you to make sure it's always included. Since you're using the function in a pipeline, you can't do anything that requires a function anyway, so there's not a major cost to doing so.




        I wouldn't go quite as far as to say this is a bug. One possible reason to behave in this way is to make command substitution $(...), which also runs a subshell, behave appropriately:



        foo=$(echo|test)


        shouldn't result in the diagnostic message being stored in foo, so that pipeline failures result in empty expansions. Another is as a way to temporarily suppress the diagnostic messages deliberately.






        share|improve this answer













        This diagnostic message is generated by the interactive shell's job control system, for the benefit of the user - it's not from the underlying program that crashed. When you pipe into a shell function a subshell is spawned to run the function, and this subshell is not treated as user-facing. If you call the function normally, it runs within the original shell, and the message is printed.



        You can test this out by disabling job control in your current shell



        set +m


        and then running ./binary again: now it won't print anything there either. Re-enable job control with set -m.



        Even a bare subshell has the same effect:



        ( : ; ./binary )


        will print no diagnostic (two commands are required in there to avoid a subshell-eliding optimisation). Piping out of the function does it too.



        Job control is disabled in the subshell, and even with it enabled manually, it's silenced. This is an unfortunate gap in the system. In a non-interactive shell the message would always be reported through a different mechanism, and anywhere else in an interactive shell it would as well.




        If printing the diagnostic is important to you, making a script instead of a function will allow you to make sure it's always included. Since you're using the function in a pipeline, you can't do anything that requires a function anyway, so there's not a major cost to doing so.




        I wouldn't go quite as far as to say this is a bug. One possible reason to behave in this way is to make command substitution $(...), which also runs a subshell, behave appropriately:



        foo=$(echo|test)


        shouldn't result in the diagnostic message being stored in foo, so that pipeline failures result in empty expansions. Another is as a way to temporarily suppress the diagnostic messages deliberately.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Michael HomerMichael Homer

        50.9k8141178




        50.9k8141178



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512321%2fsegmentation-fault-output-is-suppressed-when-piping-stdin-into-a-function-why%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

            2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

            Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?