Determining the exit status of the “return” built-in











up vote
3
down vote

favorite












I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,



#!/bin/bash
seeOutput=`return`
echo $seeOutput


It's just returning a new line and when done on terminal, it says,



-bash: return: can only `return' from a function or sourced script


which I already know :p I just need to know "return"'s exit status.










share|improve this question
























  • return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
    – Valentin Bajrami
    Aug 6 '14 at 12:35










  • There's a good answer on StackOverflow for this question.
    – garethTheRed
    Aug 6 '14 at 12:39






  • 1




    @val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
    – Stéphane Chazelas
    Aug 6 '14 at 12:42










  • @StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
    – Valentin Bajrami
    Aug 6 '14 at 13:02










  • Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
    – Gilles
    Aug 6 '14 at 23:01















up vote
3
down vote

favorite












I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,



#!/bin/bash
seeOutput=`return`
echo $seeOutput


It's just returning a new line and when done on terminal, it says,



-bash: return: can only `return' from a function or sourced script


which I already know :p I just need to know "return"'s exit status.










share|improve this question
























  • return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
    – Valentin Bajrami
    Aug 6 '14 at 12:35










  • There's a good answer on StackOverflow for this question.
    – garethTheRed
    Aug 6 '14 at 12:39






  • 1




    @val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
    – Stéphane Chazelas
    Aug 6 '14 at 12:42










  • @StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
    – Valentin Bajrami
    Aug 6 '14 at 13:02










  • Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
    – Gilles
    Aug 6 '14 at 23:01













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,



#!/bin/bash
seeOutput=`return`
echo $seeOutput


It's just returning a new line and when done on terminal, it says,



-bash: return: can only `return' from a function or sourced script


which I already know :p I just need to know "return"'s exit status.










share|improve this question















I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,



#!/bin/bash
seeOutput=`return`
echo $seeOutput


It's just returning a new line and when done on terminal, it says,



-bash: return: can only `return' from a function or sourced script


which I already know :p I just need to know "return"'s exit status.







bash shell exit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Aug 6 '14 at 12:27









Keyshov Borate

67911124




67911124












  • return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
    – Valentin Bajrami
    Aug 6 '14 at 12:35










  • There's a good answer on StackOverflow for this question.
    – garethTheRed
    Aug 6 '14 at 12:39






  • 1




    @val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
    – Stéphane Chazelas
    Aug 6 '14 at 12:42










  • @StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
    – Valentin Bajrami
    Aug 6 '14 at 13:02










  • Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
    – Gilles
    Aug 6 '14 at 23:01


















  • return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
    – Valentin Bajrami
    Aug 6 '14 at 12:35










  • There's a good answer on StackOverflow for this question.
    – garethTheRed
    Aug 6 '14 at 12:39






  • 1




    @val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
    – Stéphane Chazelas
    Aug 6 '14 at 12:42










  • @StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
    – Valentin Bajrami
    Aug 6 '14 at 13:02










  • Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
    – Gilles
    Aug 6 '14 at 23:01
















return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
– Valentin Bajrami
Aug 6 '14 at 12:35




return defaults to true. bash 4.3 has included return -1 which means error. Thus return accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
– Valentin Bajrami
Aug 6 '14 at 12:35












There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39




There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39




1




1




@val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
– Stéphane Chazelas
Aug 6 '14 at 12:42




@val0x00ff, no, return defaults to return "$?" (that is, it returns with the exit status of the last run command).
– Stéphane Chazelas
Aug 6 '14 at 12:42












@StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
– Valentin Bajrami
Aug 6 '14 at 13:02




@StéphaneChazelas What I was meaning to say. Usually return on its own indicates true as you'd do in c return 0 to indicate success. Anything else will be false and ofcourse based on the status of $? returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 Will hit the second return which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2 will hit the first return which will be true.
– Valentin Bajrami
Aug 6 '14 at 13:02












Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
– Gilles
Aug 6 '14 at 23:01




Since you know that you can only call return from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return causes its context to exit, there's no way to observe any exit status of the return instruction itself, what you observe is the exit status of the containing function or sourced script.
– Gilles
Aug 6 '14 at 23:01










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










return code is stored in $? variable.



false ; echo $?
true ; echo $?


would return



1
0


unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.



Do not mistake output and return code.






share|improve this answer




























    up vote
    4
    down vote













    What you're doing is calling a shell command return which doesn't make sense.
    In general, return with no value followed returns the exit status of the last command executed.



    From man: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.






    share|improve this answer





















    • But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
      – Keyshov Borate
      Aug 6 '14 at 12:48


















    up vote
    -1
    down vote













    This is how I used it:



    f()
    {
    ls $AAA
    return $?
    }

    g()
    {
    f
    return $?
    }

    d()
    {
    g
    echo $?
    }

    AAA=
    d


    _



    <contents of dir>
    0


    _



    AAA=sdsasdasd
    d


    _



    ls: sdsasdasd: No such file or directory
    2





    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: 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%2f148723%2fdetermining-the-exit-status-of-the-return-built-in%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      return code is stored in $? variable.



      false ; echo $?
      true ; echo $?


      would return



      1
      0


      unix convention is that 0 means OK.
      in your exemple, seeOuput hold whatever output from the back quoted command.



      Do not mistake output and return code.






      share|improve this answer

























        up vote
        3
        down vote



        accepted










        return code is stored in $? variable.



        false ; echo $?
        true ; echo $?


        would return



        1
        0


        unix convention is that 0 means OK.
        in your exemple, seeOuput hold whatever output from the back quoted command.



        Do not mistake output and return code.






        share|improve this answer























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          return code is stored in $? variable.



          false ; echo $?
          true ; echo $?


          would return



          1
          0


          unix convention is that 0 means OK.
          in your exemple, seeOuput hold whatever output from the back quoted command.



          Do not mistake output and return code.






          share|improve this answer












          return code is stored in $? variable.



          false ; echo $?
          true ; echo $?


          would return



          1
          0


          unix convention is that 0 means OK.
          in your exemple, seeOuput hold whatever output from the back quoted command.



          Do not mistake output and return code.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 6 '14 at 12:36









          Archemar

          19.4k93468




          19.4k93468
























              up vote
              4
              down vote













              What you're doing is calling a shell command return which doesn't make sense.
              In general, return with no value followed returns the exit status of the last command executed.



              From man: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.






              share|improve this answer





















              • But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
                – Keyshov Borate
                Aug 6 '14 at 12:48















              up vote
              4
              down vote













              What you're doing is calling a shell command return which doesn't make sense.
              In general, return with no value followed returns the exit status of the last command executed.



              From man: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.






              share|improve this answer





















              • But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
                – Keyshov Borate
                Aug 6 '14 at 12:48













              up vote
              4
              down vote










              up vote
              4
              down vote









              What you're doing is calling a shell command return which doesn't make sense.
              In general, return with no value followed returns the exit status of the last command executed.



              From man: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.






              share|improve this answer












              What you're doing is calling a shell command return which doesn't make sense.
              In general, return with no value followed returns the exit status of the last command executed.



              From man: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 6 '14 at 12:38









              csny

              6632721




              6632721












              • But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
                – Keyshov Borate
                Aug 6 '14 at 12:48


















              • But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
                – Keyshov Borate
                Aug 6 '14 at 12:48
















              But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
              – Keyshov Borate
              Aug 6 '14 at 12:48




              But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
              – Keyshov Borate
              Aug 6 '14 at 12:48










              up vote
              -1
              down vote













              This is how I used it:



              f()
              {
              ls $AAA
              return $?
              }

              g()
              {
              f
              return $?
              }

              d()
              {
              g
              echo $?
              }

              AAA=
              d


              _



              <contents of dir>
              0


              _



              AAA=sdsasdasd
              d


              _



              ls: sdsasdasd: No such file or directory
              2





              share|improve this answer

























                up vote
                -1
                down vote













                This is how I used it:



                f()
                {
                ls $AAA
                return $?
                }

                g()
                {
                f
                return $?
                }

                d()
                {
                g
                echo $?
                }

                AAA=
                d


                _



                <contents of dir>
                0


                _



                AAA=sdsasdasd
                d


                _



                ls: sdsasdasd: No such file or directory
                2





                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  This is how I used it:



                  f()
                  {
                  ls $AAA
                  return $?
                  }

                  g()
                  {
                  f
                  return $?
                  }

                  d()
                  {
                  g
                  echo $?
                  }

                  AAA=
                  d


                  _



                  <contents of dir>
                  0


                  _



                  AAA=sdsasdasd
                  d


                  _



                  ls: sdsasdasd: No such file or directory
                  2





                  share|improve this answer












                  This is how I used it:



                  f()
                  {
                  ls $AAA
                  return $?
                  }

                  g()
                  {
                  f
                  return $?
                  }

                  d()
                  {
                  g
                  echo $?
                  }

                  AAA=
                  d


                  _



                  <contents of dir>
                  0


                  _



                  AAA=sdsasdasd
                  d


                  _



                  ls: sdsasdasd: No such file or directory
                  2






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 26 '15 at 21:52









                  Bohdan

                  992




                  992






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f148723%2fdetermining-the-exit-status-of-the-return-built-in%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

                      Accessing regular linux commands in Huawei's Dopra Linux

                      Can't connect RFCOMM socket: Host is down

                      Kernel panic - not syncing: Fatal Exception in Interrupt