Take the 2-minute tour ×

let's say, I can rsh to machine XXX as user foo, then after executing:

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

I found that the line feed is removed in the result, and I can't see the result in a line by line way. so what I should do if I'd like to have the line feed int the returned result?

share|improve this question
You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless. –  tripleee Mar 12 '12 at 8:05
add comment

2 Answers

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

echo "$result"
share|improve this answer
add comment

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)"
share|improve this answer
add comment

Your Answer


