run complex remote shell cmds over ssh, without escaping quotes

ssh host -l user $(<cmd.txt)
Much simpler method. More portable version: ssh host -l user "`cat cmd.txt`"

2010-06-04 17:47:00

2 Alternatives + Submit Alt

What Others Think

so we need to save the command in cmd.txt first and then run it? there's no way to do this? : ssh host -l user $(perl -ane "print $F[1]\n" filename )
alperyilmaz · 593 weeks and 1 day ago
one alternative I can think of (not to save a file and then use "cat cmd.txt") is; ssh host -l user "`cat -`" And then type your command, press Enter and then ctrl-D. This will allow you run complex commands on remote machine without saving a file..
alperyilmaz · 593 weeks ago
Nice one, thanks!
DaveQB · 592 weeks and 6 days ago
Couldn't you just do, ssh -t user@host cat cmd.exe It has something to do with the -t, I'll have to re-read the man pages beucase I forgot all of what it does.
Dark006 · 592 weeks and 4 days ago
Oops my bad, nevermind my comment above. I used the -t in my command before to attach to a screen.
Dark006 · 592 weeks and 4 days ago
What about `ssh user@host < cmd.txt`?
tlrobinson · 591 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: