Search code examples
bashexit-codebash-function

return value from subshell and output to local variables


I've found the strange behaviour for me, which I can't explain. The following code is work OK:

function prepare-archive {
blah-blah-blah...
_SPEC_FILE=$(check-spec-file "$_GIT_DIR/packaging/")
exit $?
blah-blah-blah...
}

means I get value which I expect:

bash -x ./this-script.sh:
++ exit 1
+ _SPEC_FILE='/home/likern/Print/Oleg/print-service/packaging/print-service.spec
/home/likern/Print/Oleg/print-service/packaging/print-service2.spec'
+ exit 1

As soon as I add local definition to variable:

local _SPEC_FILE=$(check-spec-file "$_GIT_DIR/packaging/")

I get following:

bash -x ./this-script.sh:
++ exit 1
+ local '_SPEC_FILE=/home/likern/Print/Oleg/print-service/packaging/print-service.spec
/home/likern/Print/Oleg/print-service/packaging/print-service2.spec'
+ exit 0
$:~/MyScripts$ echo $?
0

Question: Why? What has happened? Can I catch output from subshell to local variable and check subshell's return value reliably?

P.S.: prepare-archive is called in the main shell script. The first exit is the exit from check-spec-file function, the second from prepare-archive function - this function itself is executed from main shell script. I return value from check-spec-file by exit 1, then pass this value to exit $?. Thus I expect they should be the same.


Solution

  • From the bash manual, Shell Builtin Commands section:

    local:
        [...]The return status is zero unless local is used outside a function, an invalid name is supplied, or name is a readonly variable. 
    

    Hope this helps =)