How to call a shell function

13,663

Solution 1

The exit code is contained in $?:

fun 2
a=$?

Solution 2

you may have a look at the following question: https://stackoverflow.com/questions/8742783/returning-value-from-called-function-in-shell-script with a long and well explained answer. Regards.

Share:
13,663

Related videos on Youtube

g4ur4v
Author by

g4ur4v

N00B.

Updated on September 18, 2022

Comments

  • g4ur4v
    g4ur4v almost 2 years

    How can I use the integer value returned by a function in shell script that takes some arguments as input?

    I am using the following code:

    fun()
    {
        echo "hello ${1}"
        return 1
    }
    
    a= fun 2
    echo $a
    

    I am not sure how should I call this function. I tried the methods below, bot none of them seems to work:

    a= fun 2
    a=`fun 2`
    a=${fun 2}
    
  • Wildcard
    Wildcard over 8 years