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`"

32
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 · 440 weeks and 4 days 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 · 440 weeks and 3 days ago
Nice one, thanks!
DaveQB · 440 weeks and 2 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 · 440 weeks ago
Oops my bad, nevermind my comment above. I used the -t in my command before to attach to a screen. http://www.commandlinefu.com/commands/view/2844/ssh-and-attach-to-a-screen-in-one-line.#comment
Dark006 · 440 weeks ago
What about `ssh user@host < cmd.txt`?
tlrobinson · 439 weeks 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: