Search code examples
bashechostderr

BASH script echo text and a command


This is seriously lame I know but...Im pulling my hair out on it.

Basically I Have this code:

getDisplayName(){
echo Display Name: $(/opt/vc/bin/tvservice  -n)
}

It returns this:

[E] No device present
Display Name:

I would expect it to be:

Display Name: [E] No Device Present

What am I missing?

TIA Ron


Solution

  • Looks pretty much like the error message from /opt/vc/bin/tvservice goes to the standard error stream (not standard output), so it is not captured by $(). Try redirecting to standard output using 2&>1 2>&1

    getDisplayName(){
      echo Display Name: $(/opt/vc/bin/tvservice  -n 2>&1)
    }