[ale] bash return and exit

James Sumners james.sumners at gmail.com
Tue Sep 17 12:21:50 EDT 2013


http://tldp.org/LDP/abs/html/complexfunct.html

Note that a Bash "function" essentially defines a new "command". So,
if you have defined a function that returns a status, as in your first
declaration, then you would do the following:

~~~
function my_function() {
  do_something
  return $?
}

my_function
MY_VAR=$?
~~~

On Tue, Sep 17, 2013 at 12:06 PM, leam hall <leamhall at gmail.com> wrote:
> I've looked at a few google searches and am not sure I understand what I'm
> seeing. In bash, I want to have a function do a test, and have a variable in
> the calling program set based on the function's actions. So far it seems as
> if I have to either "echo" the result or have the calling function use $?.
>
> Calling script:
>
> MY_VAR=`my_function`
>
> # This fails:
> my_function() {
>   do_something
>   return $?
> }
>
> # This works:
> my_function() {
>   do_something
>   if [ $? -eq 0 ]
>     echo 0
>   else
>     echo 1
>    fi
> }
>
> The first function works if the parent's calls:
>
> MY_VAR=`my_function; echo $?`
>
> Am I looking for something that's just not in bash?
>
> Leam
>
>
> --
> Mind on a Mission
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
James Sumners
http://james.roomfullofmirrors.com/

"All governments suffer a recurring problem: Power attracts
pathological personalities. It is not that power corrupts but that it
is magnetic to the corruptible. Such people have a tendency to become
drunk on violence, a condition to which they are quickly addicted."

Missionaria Protectiva, Text QIV (decto)
CH:D 59


More information about the Ale mailing list