3

If I assign the output of a command substitution to a local variable, how do I get the exit status of the command?

This is the behaviour of ZSH 5.8:

false; echo $? # output is 1 as expected

foo=$(false); echo $? # output is 1 as expected

local foo=$(false); echo $? # output is 0
Jay
  • 245

1 Answers1

6

Split the declaration from the assignment:

local foo
foo=$(false)

(See also SC2155.)

Stephen Kitt
  • 434,908
  • Also local foo=$(false) status=$? works to save the exit status of the subshell – Moh Sep 17 '23 at 05:07