35

I am setting a variable like this:

myvar=$(command --params)

I want to check the exit code ($?) of my command afterwards. Checking $? like this always returns 0 because it successfully set the variable to the output of the command.

Is it possible to get the return value of command?

Questionmark
  • 3,945

1 Answers1

46

Yes, it is possible without even getting too far out of your way:

$ $(exit 3); echo $?
3

$ foo="$(echo bar; exit 3)"; echo $?; echo $foo
3
bar
DopeGhoti
  • 76,081