In my .bashrc I define a function which I can use on the command line later:
function mycommand() {
ssh [email protected] cd testdir;./test.sh "$1"
}
When using this command, just the cd command is executed on the remote host; the test.sh command is executed on the local host. This is because the semicolon separates two different commands: the ssh command and the test.sh command.
I tried defining the function as follows (note the single quotes):
function mycommand() {
ssh [email protected] 'cd testdir;./test.sh "$1"'
}
I tried to keep the cd command and the test.sh command together, but the argument $1 is not resolved, independent of what I give to the function. It is always tried to execute a command
./test.sh $1
on the remote host.
How do I properly define mycommand, so the script test.sh is executed on the remote host after changing into the directory testdir, with the ability to pass on the argument given to mycommand to test.sh?
&&instead of the semi-colon to join the commands executed on the remote host:cd testdir && ./test.sh "$1". With that form (because evaluation of&&short-circuits in bash), if thecdfails the second command won't be executed, and you won't inadvertently run a differenttest.shinuser's homedir.