Calling one function within another function

Issue

I am trying to call one bash function from within another bash function and it is not working as expected :

#/bin/bash
function func1(){
    echo "func1 : arg = ${1}"
    return 1
}
function func2(){
    echo "func2 : arg = ${1}"
    local var=func1 "${1}"
    echo "func2 : value = $var"
}
func2 "xyz"

and the current output is :

Current output :
func2 : arg = xyz
func2 : value = func1

Question : how can I modify the program above so as to get the following output ? :

Desired output : 
func2 : arg = xyz
func1 : arg = xyz
func2 : value = 1

Solution

Change func2 definition to the following:

function func2 () {
    echo "func2 : arg = ${1}"
    func1 "${1}"
    local var=$?
    echo "func2 : value = $var"
}

Answered By – choroba

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published