68

Is there any clever way to run a local Bash function on a remote host over ssh?

For example:

#!/bin/bash
#Definition of the function
f () {  ls -l; }

#I want to use the function locally
f

#Execution of the function on the remote machine.
ssh user@host f

#Reuse of the same function on another machine.
ssh user@host2 f

Yeah, I know it doesn't work, but is there a way to achieve this?

3 Answers 3

138

You can use the typeset command to make your functions available on a remote machine via ssh. There are several options depending on how you want to run your remote script.

#!/bin/bash
# Define your function
myfn () {  ls -l; }

To use the function on the remote hosts:

typeset -f myfn | ssh user@host "$(cat); myfn"
typeset -f myfn | ssh user@host2 "$(cat); myfn"

Better yet, why bother with pipe:

ssh user@host "$(typeset -f myfn); myfn"

Or you can use a HEREDOC:

ssh user@host << EOF
    $(typeset -f myfn)
    myfn
EOF

If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:

ssh user@host "$(typeset -f); myfn"

Explanation

typeset -f myfn will display the definition of myfn.

cat will receive the definition of the function as a text and $() will execute it in the current shell which will become a defined function in the remote shell. Finally the function can be executed.

The last code will put the definition of the functions inline before ssh execution.

Sign up to request clarification or add additional context in comments.

10 Comments

Better to use typeset -f f and only send the definition of the one function across
@HenkLangeveld - It depends whether there are required functions called by f(). In my assumption, the function f() might need other functions. Otherwise your suggestion would be best.
The same way you would any commands. If the function is f() then you can pass parameters like f param1 param2 .... Inside f() you will reference the parameters as $1, $2, ... $n.
Excellent! I used declare -f instead of typeset -f for Bash. THanks.
I get a syntax error near unexpected token ; when using either declare -f or typset -f
|
7

I personally don't know the correct answer to your question, but I have a lot of installation scripts that just copy themselves over using ssh.

Have the command copy the file over, load the file functions, run the file functions, and then delete the file.

ssh user@host "scp user@otherhost:/myFile ; . myFile ; f ; rm Myfile"

Comments

6

Another way:

#!/bin/bash
# Definition of the function
foo () {  ls -l; }

# Use the function locally
foo

# Execution of the function on the remote machine.
ssh user@host "$(declare -f foo);foo"

declare -f foo print the definition of function

3 Comments

declare: not found ,
how to call multiple functions?
@Varsh It's possible to transmit multiple functions like this: $(declare -f bar foo); foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.