I'd like to be able to declare a local var as the result of a command AND to be able to capture return status of command.

Something like this:

typeset -r command_output=$(command)
typeset -r return_status=$?
echo "$return_status" # always 0

The problem with the snippet code above is return_status will always be 0, even if command exit with another status code.

Any one to help me ?

1 Answer 1


The problem is that the typeset creates the return status. This code (i.e. both assignments in the same declaration) works for me:

typeset -r command_output=$(command) return_status=$?

To keep the return_status writable (not read-only) you can do:

typeset    command_output=$(command) return_status=$?
typeset -r command_output

(i.e. declare the read-only variable as the subsequent command).

  • You are right Janis, thank you very much ! What if I want return_status not to be a constant (to be reusable ?) Commented Mar 24, 2015 at 22:01
  • For your additional question I added the solution to my answer.
    – Janis
    Commented Mar 24, 2015 at 22:09
  • Easy to do, thanks for pointing me this complete solution Commented Mar 24, 2015 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .