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 ?


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 ?) – pierrefevrier Mar 24 '15 at 22:01
  • For your additional question I added the solution to my answer. – Janis Mar 24 '15 at 22:09
  • Easy to do, thanks for pointing me this complete solution – pierrefevrier Mar 24 '15 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.