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

How is it possible to execute a shell script without creating a file? For example assume I have the following script (testscript):

function run_free() {
   free -m


I then of course can execute this with: sh testscript

I want to ovoid creating a file though. I tried:

sh echo '#!/bin/bash function run_free() { free -m } run_free'

Which did not work. Any ideas?

share|improve this question
Why would you want to do this - what are you trying to achieve ? – Iain Jun 21 '12 at 7:00
Its a long story, basically the shell scripts are being passed via SSH, so there is no need to create a file and add +x. I want to simply execute the shell script string. – Justin Jun 21 '12 at 7:01
Just a note: the issue you identified in your clarification was not the question you asked. In general, state your constraints and your goals. Not your presumed methods (which in this case really don't make much sense). – Dr. Edward Morbius Jun 27 '12 at 22:47
up vote 9 down vote accepted

Most interpreters have a parameter to specify some code to execute. You can use this to invoke a specific interpreter and provide the code. For example:

bash -c 'function run_free() { free -m; }; run_free'

(Note that you need some semicolons in there since you don't have newlines.)

perl uses -e, and python uses -c.

share|improve this answer

Yo dawg, I heard you like shell, so I put shell in your shell so can shell while you shell.

In other words, you are spawning a shell for your echo command and then just echoing everything in one line. Shell interprets that as just an echo command. Even if it did not, the fact that everything is in one line (especially that there is no new line after #!/bin/bash) causes problems.

While typing this reply, I found out from your comment you are going to run commands over ssh. Then something like this should work:

ssh your_user@yourserver "function run_free() { free -m } run_free"
share|improve this answer
Don't we need to specify the shebank? What if we wanted to pass a perl script? – Justin Jun 21 '12 at 7:05
Why didn't you say you wanted to run perl scripts too ? – Iain Jun 21 '12 at 7:10
@Justin: If you want to run Perl over ssh, just use ssh your_user@yourserver "perl" or ssh your_user@yourserver "perl -e 'your Perl code goes here'". – Janne Pikkarainen Jun 21 '12 at 7:44
@Justin The shebang is only used in shell scripts, i.e. in files, and is not processed by the shell itself. In fact, the shell ignores it because it is a comment (it starts with '#'). That's the reason why bash -c '#!/bin/bash echo hello' won't output anything: bash ignores the whole command because it is a comment. – daniel kullmann Jun 21 '12 at 7:50

You can use here documents to feed SSH scripts:

ssh -T myhost <<EOF

The '-T' option disables TTY allocation.

I use this technique to run a standard script across multiple user accounts on one of our 3rd-party services which doesn't allow for a commonly-accessible writable bin directory. The SSH here-document method obviates the need to copy (and maintain) the same script across multiple accounts.

share|improve this answer

You could create the scripts on the ssh originating system and pipe them into the ssh session

ssh user@somehost.tld /bin/bash <local.script

Putting your example in a local script run_free

ssh somehost.tld /bin/bash <run_free
Pseudo-terminal will not be allocated because stdin is not a terminal.
         total       used       free     shared    buffers     cached
Mem:           371        246        124          0         49         80
-/+ buffers/cache:        116        254
Swap:          767          0        767

runs the local script on the remote host.

You can do the same thing with perl

ssh user@somehost.tld /usr/bin/perl <perl.script
share|improve this answer
Can you send arguments to that script? – ChocoDeveloper Aug 4 '12 at 0:13

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.