Question

I need to "cat" a text inside shell script to remote machine via ssh. This is required for simplicity, so i don't need to keep extra file.

For instance

#!/bin/sh
VAR="some"
VAR1="something"

cat << EOF
apple
green
tree
EOF   ---> cat to file.txt on remote machine

do some command
do some command1
exit 0
Was it helpful?

Solution

Try something like this if you're generating file content in runtime:

cat <<EOF | ssh remote 'cat - > /tmp/my_remote_file.txt'
apple
green
tree
EOF

Or simply use scp if file is static.

OTHER TIPS

Just want to note for everyone. Above example works fine for plain text, but if heredoc includes variables then substitution will take place. So important to protect heredoc phrase with single quotes to keep everything intact For example

cat <<'EOF' | ssh remote 'cat - > /tmp/my_remote_file.txt'
host=$(uname -a)
echo $host
EOF

host variable won't be substituted with actual hostname. As reference (paragraph 19-7) http://tldp.org/LDP/abs/html/here-docs.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top