how to keep line feed in result returned by bash command

Solution 1:

The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)

result=$(rsh -l foo XXX "ls")
echo "$result"

In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.

If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo

Solution 2:

Edit: If you mean the newlines between each file, just make sure to quote the result:

result="$(rsh -l foo XXX "ls");"

This will keep any characters except NUL ($'\0'), which can't be stored in a Bash variable.

If you mean that the trailing newline is lost, then this is the simplest solution I know:

resultx="$(commands...; echo x)"
result="${resultx%x}"