Hacker News new | ask | show | jobs
by racino84 2435 days ago

  ssh user@domain.xxx << HERE
    echo "HELLO"
    cat /etc/hosts
  HERE
1 comments

Ah, hear documents -- had the arrows pointing the other way. The problem is that you still have to worry about escaping some special characters, such as the dollar sign.

Whereas by using the "declare" syntax, or the attached script in the gist, you can send a number of inter-dependent functions without escaping anything in them (treat them as if they are local), along with the additional complexity you can encapsulate into regular structured programming syntax.

You can control expansion of heredoc content by quoting the delimiter, like:

  ssh user@domain.xxx <<'HERE'
    echo $USER
  HERE
Per POSIX, "If any part of word is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself." "If no part of word is quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion."