9

I'd like to execute a command and script located on a remote machine with a script on a local machine. I know it's possible to execute these kind of commands with ssh, so I made:

#!/bin/bash
ssh username@target 'cd locationOf/theScript/; ./myScript.sh'

It works perfectly. I'd like this script to be more generic, using variables. Now, it is:

#!/bin/bash
LOCATION=locationOf/theScript/
EXEC=myScript.sh
ssh username@target 'cd ${LOCATION}; ./${EXEC}'

And I get this error: bash: ./: is a directory

I guess the remote machine doesn't know these variables. So is there a way to export them to the target ?

  • 1
    See also [Using exec in find over ssh from shell script](http://unix.stackexchange.com/questions/139766/using-exec-in-find-over-ssh-from-shell-script/139800#139800), [How can I run a script immediately after connecting via SSH?](http://unix.stackexchange.com/questions/9883/how-can-i-run-a-script-immediately-after-connecting-via-ssh), [How can I set environment variables for a remote rsync process?](http://superuser.com/questions/207200/how-can-i-set-environment-variables-for-a-remote-rsync-process) – Gilles 'SO- stop being evil' Jul 04 '14 at 00:24

1 Answers1

10

I don't know an easy way of exporting environmental variables to target, but your script might work if you replace ' with ". With 's the string 'cd ${LOCATION}; ./${EXEC}' gets passed verbatim, but with

ssh username@target "cd ${LOCATION}; ./${EXEC}"

variable substitution is done locally.

Note that the values of LOCATION and EXEC are passed to the remote shell, so this only works if they don't contain any shell special characters.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
isido
  • 151
  • 1
  • 3