Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Oh yes, it does ! Thank you :) –  Jean-Baptiste Martin Jul 2 at 12:16

Your Answer

 
discard

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.