I want to store the output of a bash command to a string in a bash script. The part that matters is as follows:

player_status="$(playerctl -l)"

The output of the command when run on terminal (not with the bash script) is "No players were found". When I run the bash script (note that there is not an echo) it outputs "No players were found" to the terminal. I want it instead to not put it in the terminal but instead the variable.


It sounds like your command is producing output to standard error rather than standard output. Try using the 2>&1 modifier on your shell command.

player_status="$(playerctl -l 2>&1)"

The modifier means "send standard error to standard output."

| improve this answer | |

Please try to use backquotes ( ` ), backquotes execute the command and return the output in the same shell LEVEL

player_status=`playerctl -l`

instead of

player_status="$(playerctl -l)"   # Here the command is executed in child process thus its output is not available in this script from which CHILD process was executed
| improve this answer | |

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.