Monday, October 31, 2022

[SOLVED] 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
Answer Checked By - Timothy Miller (WPSolving Admin)