Home > Software design >  How can I print two commands on remote server in the same line?
How can I print two commands on remote server in the same line?

Time:10-14

When I'm trying to print in the console the output of this command ssh 192.168.1.120 'df -h | grep sda2; hostname;' I get this

/dev/sda2        23G  8.0G   14G  38% /
localhost

while I would like to see this

/dev/sda2        23G  8.0G   14G  38% / localhost

Although I can print these in the same line with the following way

result=`ssh 192.168.1.120 'df -h | grep sda2; hostname;'`
echo $result

It is not what I want, since this way does not preserve the actual spaces between the characters.

Is there any way to make it work?

CodePudding user response:

Usually ppl just use the fact, that command substitution removes trailing newlines.

ssh 192.168.1.120 'echo $(df -h | grep sda2) $(hostname)'

In your case, you can also postprocess it.

ssh ... | paste -sd' '
ssh ... | tr -d '\n'

Remove newline from one command:

ssh ... 'df -h | grep sda2 | tr -d "\\n" ; hostname'

Prefer $(...) over backticks `. Check your scripts with shellcheck.

  • Related