To boil it down: The following command in bash
echo $(ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html")101
Gives the this result
Connection to doctor closed.
101-data
While
echo $(ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html")
gives
Connection to doctor closed.
www-data
Why is this?
Background: I'm using this script (called ced.sh):
for ARG; do
name=${ARG##*/}
echo name=$name
path=${ARG%/*}
echo path=$path
owner=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%U\" $ARG"))
echo owner=$owner
group=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%G\" $ARG"))
echo group=$group
perms=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%a\" $ARG"))
echo perms=$perms
read -p "Gibt es einen Dienst/Server, der auf diese Datei zurückgreift (notfalls leer lassen)? " service
echo service=$service
echo "name="\"$name\" "path="\"$path/\" "owner="\"$owner\" "group="\"$group\" "perms="$perms "service="$service
done
Its purpose is to read some information (e.g. owner, group, permissions) from files on a server, which I'd like to edit locally. The last line should produce an output that can be stored in a file.
It would have been rewritten to:
echo "name="\"$name\" "path="\"$path/\" "owner="\"$owner\" "group="\"$group\" "perms="$perms "service="$service >> "$HOME/...bla bla..."
The output looks like this:
Software/EigeneProgramme/ced.sh -p /var/www/candy/example/index.html
name=index.html
path=/var/www/candy/example
Connection to doctor closed.
owner=www-data
Connection to doctor closed.
group=www-data
Connection to doctor closed.
perms=644
Gibt es einen Dienst/Server, der auf diese Datei zurückgreift (notfalls leer lassen)? apache
service=apache
service=apacheta path="/var/www/candy/example/" owner="www-data
I don't understand why echo produces the last line. It seems that it is writing to line in on. Did I forget to escape something?
There is an easy explanation for this. The values retrieved from the remote server all come with a trailing carriage return (
\r
).You were expecting this:
But
www-data
and644
are followed by a carriage return. If CR would be rendered as a newline by your terminal emulator, then the output would look like this:Without the linefeeds, lines overlap; the dots represent the characters that are overwritten.
As you already pointed out, on most terminals this looks like:
You can verify this by piping the remote output to
cat -v
; the carriage return will show up as^M
.One possible solution is to strip off the carriage return by piping the remote output through
tr -d '\r'
: