5 ms·
Isn't the >> notation easier
by racino84 7y ago
Isn't the >> notation easier
- derekp7 7y agoCan you elaborate? The only '>>' notation I'm familiar with is to append to a file. The purpose of this technique is if you have a script running on, say, a master host, has a bunch of functions, but has a couple functions that need to run somewhere else (like a backup script that needs to put a remote database server in hot backup mode). You can either write the remote commands to a file, sync it to the remote host (somewhere), then execute it. Or, use this technique to pass the function "live" to the remote environment and run it unmodified. I personally use it in the front-end client script (snebu-client) for the Snebu backup system.
- racino84 7y agossh user@domain.xxx << HERE echo "HELLO" cat /etc/hosts HERE
- derekp7 7y agoAh, 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.
- wahern 7y agoYou 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."