0

Let's say that I have a function named nested which returns a String and an error code, this function is then used by the main function to assign the returned value to a variabe.

Sample code

function main() {
    local variable=$(nested)
    echo $?
    echo $variable
}

function nested() {
    echo "Value"
    return 1
}

main

Expected output

1
Value

Actual output

0
Value

How do I fix this and/or what's the proper way to gather the exit code of a function that is enclosed by $(...) ?

Jsh0s
  • 519
  • 1
  • 8
  • 18
  • 3
    Your problem isn't the `$()`, it's the `local`. Split it into two pieces - `local variable ; variable=$( nested ) ; echo $?` – tjm3772 Mar 15 '23 at 16:14

0 Answers0