0

I have a script test.sh and I am trying to ssh and call that scrip't function in the same script:

#!/bin/sh

testme()
{
   echo "hello world"
}

ssh myserver "/opt/scripts/test.sh; testme"

But I keep getting testme command not found

What is the correct way of calling a function from a script after ssh?

4
  • 2
    have you looked at this? stackoverflow.com/q/305035/2055887 Commented Mar 11, 2019 at 15:56
  • 1
    You need to source the script, not execute it, so that the function is defined in the current shell. Commented Mar 11, 2019 at 15:58
  • Also, is the script actually on the remote host, or just the local host? In general, you can't "send" function definitions over the ssh connection. Commented Mar 11, 2019 at 15:59
  • try ssh remoteServer '/bin/bash -x -s ' < <(cat /path/on/local/server/test.sh; echo "testme") Commented Mar 11, 2019 at 16:10

1 Answer 1

1

If you use Bash on both sides, you can have it serialize the function for you:

#!/bin/bash

testme()
{
   echo "hello world"
}

ssh myserver "$(declare -f testme); testme"

If you need sh compatibility, this is not an option.

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

Comments

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.