Capture exit code and output of a command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'd like to do:



1.sh:



#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"


2.sh:



#!/usr/bin/env bash
echo output
exit 2


But it outputs:



$ ./1.sh
output
0 # I'd like to have `2` here


Since $(...) runs a separate shell. So, how do I capture both, exit code and output?










share|improve this question























  • a=$(./2.sh); r=$?; ## doesn't work?
    – Jeff Schaller
    Mar 4 '16 at 11:34










  • The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
    – Mikkel
    May 29 at 14:20














up vote
3
down vote

favorite












I'd like to do:



1.sh:



#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"


2.sh:



#!/usr/bin/env bash
echo output
exit 2


But it outputs:



$ ./1.sh
output
0 # I'd like to have `2` here


Since $(...) runs a separate shell. So, how do I capture both, exit code and output?










share|improve this question























  • a=$(./2.sh); r=$?; ## doesn't work?
    – Jeff Schaller
    Mar 4 '16 at 11:34










  • The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
    – Mikkel
    May 29 at 14:20












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'd like to do:



1.sh:



#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"


2.sh:



#!/usr/bin/env bash
echo output
exit 2


But it outputs:



$ ./1.sh
output
0 # I'd like to have `2` here


Since $(...) runs a separate shell. So, how do I capture both, exit code and output?










share|improve this question















I'd like to do:



1.sh:



#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"


2.sh:



#!/usr/bin/env bash
echo output
exit 2


But it outputs:



$ ./1.sh
output
0 # I'd like to have `2` here


Since $(...) runs a separate shell. So, how do I capture both, exit code and output?







shell command-substitution return-status






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 at 14:24









Peter Mortensen

79748




79748










asked Mar 4 '16 at 11:28









x-yuri

1,11911641




1,11911641











  • a=$(./2.sh); r=$?; ## doesn't work?
    – Jeff Schaller
    Mar 4 '16 at 11:34










  • The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
    – Mikkel
    May 29 at 14:20
















  • a=$(./2.sh); r=$?; ## doesn't work?
    – Jeff Schaller
    Mar 4 '16 at 11:34










  • The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
    – Mikkel
    May 29 at 14:20















a=$(./2.sh); r=$?; ## doesn't work?
– Jeff Schaller
Mar 4 '16 at 11:34




a=$(./2.sh); r=$?; ## doesn't work?
– Jeff Schaller
Mar 4 '16 at 11:34












The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
– Mikkel
May 29 at 14:20




The reason 1.sh has an exit code of 0 not 2 is because echo "$r" is a command that exits 0.
– Mikkel
May 29 at 14:20










1 Answer
1






active

oldest

votes

















up vote
8
down vote



accepted










The exit code of a process calling another process is the one of the called process.



$($($($($(exit 2)))))
echo $?
2


Here there are 5 levels of calling.



In your case:



r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"





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%2f267593%2fcapture-exit-code-and-output-of-a-command%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
    8
    down vote



    accepted










    The exit code of a process calling another process is the one of the called process.



    $($($($($(exit 2)))))
    echo $?
    2


    Here there are 5 levels of calling.



    In your case:



    r=0
    a=$(./2.sh)
    r=$?
    echo "$a"
    echo "$r"





    share|improve this answer


























      up vote
      8
      down vote



      accepted










      The exit code of a process calling another process is the one of the called process.



      $($($($($(exit 2)))))
      echo $?
      2


      Here there are 5 levels of calling.



      In your case:



      r=0
      a=$(./2.sh)
      r=$?
      echo "$a"
      echo "$r"





      share|improve this answer
























        up vote
        8
        down vote



        accepted







        up vote
        8
        down vote



        accepted






        The exit code of a process calling another process is the one of the called process.



        $($($($($(exit 2)))))
        echo $?
        2


        Here there are 5 levels of calling.



        In your case:



        r=0
        a=$(./2.sh)
        r=$?
        echo "$a"
        echo "$r"





        share|improve this answer














        The exit code of a process calling another process is the one of the called process.



        $($($($($(exit 2)))))
        echo $?
        2


        Here there are 5 levels of calling.



        In your case:



        r=0
        a=$(./2.sh)
        r=$?
        echo "$a"
        echo "$r"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 4 '16 at 12:10









        Jeff Schaller

        32.7k849110




        32.7k849110










        answered Mar 4 '16 at 11:36









        Emmanuel

        2,92911020




        2,92911020



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f267593%2fcapture-exit-code-and-output-of-a-command%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay