Caleb Jones Caleb Jones - 1 year ago 77
Bash Question

Echoing result of multiple shell command outputs in bash

I am trying to return the result of multiple commands into a single output line. The issue is it seems to output the commands on top of each other, only when the variables are commands.

Example 1:

Provider=$(adb shell getprop gsm.operator.alpha)
Model=$(adb shell getprop ro.product.model)
pM=$Provider $Model
echo $pM + " is connected"

Output: is connected
Expected: T-Mobile Nexus 5 is connected

a='hello '
echo $c " its pretty outside."

Works as expected, output: hello world its pretty outside.

I appreciated any assistance!

Answer Source

Lines in the output of adb shell commands are terminated by \r\n, so you have to remove the \r. There are many ways of doing it, here you are one (and some other corrections):

Provider=$(adb shell getprop gsm.operator.alpha | tr -d \\r)
Model=$(adb shell getprop ro.product.model | tr -d \\r)
pM="$Provider $Model"
echo $pM " is connected"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download