5

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

#!/bin/bash
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.

2 Answers 2

6

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.

#!/bin/bash
player_status="$(playerctl -l 2>&1)"

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

0
1

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

You must log in to answer this question.

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