Ssh – execute bash scripts through ssh

bashssh

Is there any way that ssh can specify the working directory on the remote host when executing bash scripts through ssh? So that the bash scripts can use relative path in it instead of absolute path. The below example seems not working. Please help, thanks.

Example for bash script on remote host:

./executable -o arg1 -i arg2

Example for command send on local:

ssh id@remote.hostname '/path/to/script/myscript.sh'

Best Answer

Your working directory on the target system should be the login users's home directory. run

 ssh id@server pwd

If you want to be in a particular directory before running the program, then do this:

 ssh id@server "cd /path/to/dir && pwd"

Do note the double quotes, without quotes, it will get treated as two commands.