Search code examples
linuxbashsshopensuse

Preserve '\n' newline in returned text over ssh


If I execute a find command, with grep and sort etc. in the local command line, I get returned lines like so:

# find ~/logs/ -iname 'status' | xargs grep 'last seen' | sort --field-separator=: -k 4 -g
0:0:line:1
0:0:line:2
0:0:line:3

If I execute the same command over ssh, the returned text prints without newlines, like so:

# VARcmdChk="$(ssh ${VARuser}@${VARserver} "find ~/logs/ -iname 'status' | xargs grep 'last seen' | sort --field-separator=: -k 4 -g")"
# echo ${VARcmdChk}
0:0:line:1 0:0:line:2 0:0:line:3

I'm trying to understand why ssh is sanitising the returned text, so that newlines are converted to spaces. I have not yet tried output'ing to file, and then using scp to pull that back. Seems a waste, since I just want to view the remote results locally.


Solution

  • When you echo the variable VARcmdChk, you should enclose it with ".

    $ VARcmdChk=$(ssh ${VARuser}@${VARserver} "find tmp/ -iname status -exec grep 'last seen' {} \; | sort --field-separator=: -k 4 -g")
    $ echo "${VARcmdChk}"
    last seen:11:22:33:44:55:66:77:88:99:00
    last seen:00:99:88:77:66:55:44:33:22:11
    

    Note that I've replaced your xargs for -exec.