The reason the cat
happens at the start is because you are using double quotes. The $(cat)
is evaluated and executed locally before the ssh
command runs. To prevent that, use single quotes instead. (I took the liberty to simplify a bit more at the same time.)
ssh -t root@$host "echo 'Connected to server $host'"';
echo "Paste in data and hit Ctrl + D"
data=$(cat)
echo "Success!"
echo "$data"'
(The first echo
is in double quotes in order to allow $host
to be expanded locally; then we switch to single quotes to protect the rest of the command line from local expansion.)