How to Store Shell Script Output to a Variable

bashcommand-substitutiongithub

I have read many threads on this topic, but none of the solutions have worked for me.

I am trying to do the following:

RELEASE_COMMAND_OUTPUT=$(exec ~/temp/execs/github-release release --user patick --repo $REPO_NAME --tag $RELEASE_VERSION --name $RELEASE_VERSION --description "$DESC")

but the output of ./github-release is not stored in the RELEASE_COMMAND_OUTPUT variable.

I'm aware of the concept of command substitution, but none of the solutions I've tried have worked. What am I doing wrong?

Best Answer

As explained in your question's comments, the variable RELEASE_COMMAND_OUTPUT will only get the STDOUT(standard output) of your command but not the STDERR (standard error), as it seems to be your case.

When in doubt about the output of your command you can use process substitution to mark the STDERR in red as in the following example:

command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)

Which is redirecting (the 2> part) the STDERR of the command as input to the while block which will print it in red in the terminal.

You can also do the opposite, marking the STDOUT as red with:

command | grep .

Because grep act only in STDOUT

NOTE: in some systems you may need to enable the color in grep using the flag --color=auto. On Macos and Linux usually it's enable by default.

Related Question