Suppose I'm using a shell like bash or zsh, and also suppose that I have a command which writes to stdout. I want to capture the output of the command into a shell variable, and also to capture the command's return code into another shell variable.
I know I can do something like this ...
command >tempfile
rc=$?
output=$(cat tempfile)
Then, I have the return code in the 'rc' shell variable and the command's output in the 'output' shell variable.
However, I want to do this without using any temporary files.
Also, I could do this to get the command output ...
output=$(command)
... but then, I don't know how to get the command's return code into any shell variable.
Can anyone suggest a way to get both the return code and the command's output into two shell variables without using any files?
Thank you very much.