Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have scripts in several languages which I pipe to ssh to have them invoked on other hosts. For shell scripts I'm able to do this:

ssh < /path/to/

For scripts in other languages however, I find myself having to do this:

ssh python - < /path/to/

Locally I can run the scripts without specifying the interpreter, due to their shebang lines, and executable bits.

How can I have ssh execute scripts regardless of language by invoking the appropriate interpreter from the shebang? Is there something I can have ssh invoke regardless of scripts language to do this?


The ssh invocation is part of yet another script which I am developing, this is strictly specific to coding and not server management.

share|improve this question

migrated from Jul 20 '11 at 9:31

This question came from our site for professional and enthusiast programmers.

Could you invoke a command that reads it's input into a temporary file and executes that? Something like

ssh user@host '{ F=$(mktemp /tmp/scripts.XXXXXX); cat > $F ; chmod +x $F; $F; rm $F }' <
share|improve this answer
curious as to why you have the whole command in {}? – Matt Joiner Jul 20 '11 at 1:07
Overcorrection? :-) – thsutton Jul 20 '11 at 1:08
No comprendo, please explain :P – Matt Joiner Jul 20 '11 at 3:26
It doesn't seem to be required in this case (though this might be a bash-ism), but I'm used to wrapping groups that need descriptor redirection in {}. Like people who use too many commas, correct the spelling of already correct words (thereby misspelling them), etc. :-) – thsutton Jul 20 '11 at 4:06

You can't. The shebang line is recognized by the OS when it's exec-ing a file. When you're piping it over ssh, you're just sending commands to the shell (/bin/sh) or whatever other command you specified should be run.

The only way to have the remote server recognize the shebang is to copy the files to the remote server and set the execute bit.

You could fake it by writing a wrapper around ssh that looks for a shebang line in standard input to determine what command to run, or by doing some trickery to make non-sh scripts run under sh, like the classic tcl idiom

# \
exec tclsh "$0" "$@"

(I don't know the equivalent for other languages)

share|improve this answer
that's a nice idea – Matt Joiner Jul 20 '11 at 1:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.