How to execute echo $ command without breaking layout

I am trying to do the following in a bash script:

com=`ssh host "ls -lh"`

echo $com

This works, but the echo will interrupt the output (instead of getting all the rows in the column, I get them all in a row).

If I do: ssh host ls -lhin the CLI, it will give me the correct output and layout.

How to save layout when repeating a variable?

+3
source share
2 answers

You need:

echo "$com"

The quotation marks force the shell not to break the meaning into "words", but to pass it as one argument to echo.

+2
source

Put double quotes around $ com:

com=`ssh host "ls -lh"`
printf "%s" $com | tr -dc '\n' | wc -c   # count newlines
printf "%s" "$com" | tr -dc '\n' | wc -c
echo "$com"
+1
source

Source: https://habr.com/ru/post/1742125/


All Articles