[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Return status of command substitution with $(...) "gets lost"

From: Ettelbrueck, Heiko
Subject: Return status of command substitution with $(...) "gets lost"
Date: Thu, 4 Mar 2010 08:36:26 +0100

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
-DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale'
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib
  -g -O2
uname output: Linux wdfd00221495a #1 SMP 2010-01-06
16:07:25 +0100 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.1
Patch Level: 0
Release Status: release

        There is a function which runs some external tool. Both the
        tool's output and its exit status are relevant for further
        execution of the bash script.
        -> The tool's is stored into a (new) local variable, which is
           then processed further.
        -> The tool's exit status is retrieved from the $? variable.

        Important detail: The local variable is declared and defined in
        the same step with "local VARNAME=$(do something)".

        Problem: The $? variable is always 0 after that statement. (If,
        on the other hand, I separate the declaration and the
        definition of the variable as shown in the example below, the
        $? variable is really set to the exit status of the external

        I realized this on version 4.1 (as this bug is reported for
        now) as well as on older versions (4.0, 3.x), if I remember it

        Please check whether that behavior is intended or whether it's
        really a bug.

        The following script contains two functions, the first one
        showing the scenario where the issue occurs (testfunc_fail),
        the second one showing the working scenario (testfunc_ok).

        testfunc_fail() {
          local OUTPUT=$(touch /does/not/exist 2>&1)
          echo "testfunc_fail() returned $? with message '$OUTPUT'."

        testfunc_ok() {
          local OUTPUT
          OUTPUT=$(touch /does/not/exist 2>&1)
          echo "testfunc_ok() returned $? with message '$OUTPUT'."


reply via email to

[Prev in Thread] Current Thread [Next in Thread]