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
  • 2
  • 6

1 Answers1

6

Split the declaration from the assignment:

local foo
foo=$(false)

(See also SC2155.)

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164