I am trying to run a script on a remote system using ssh, when i run the command on the terminal it executes fine but when i embed the command in a shell script it returns an error.

Command 1:

cmd="ssh root@IP \"python /pathtoscript\""
`$cmd` > log.log

for the above command i tried the following suggestions from stack exchange:

 cmd="ssh root@IP \"bash -c 'python /pathtoscript'"
 cmd="ssh root@IP '/user/bin/python /pathtoscript'"

Command 2:

ssh root@IP "ls -1v /path"

Note: All these commands work on the terminal.


If I run the command on the terminal it executes fine and displays the output. Now if the same command is added to a script (.sh), I see the following error:

bash: ssh root@IP 'python /pathtoscript' : No such file or directory
  • 3
    I don't see the use of backquote. You are trying to execute a program named ssh root@IP 'python /pathtoscript' which is probably not what you want. simply run $cmd
    – Archemar
    Commented Feb 8, 2017 at 10:07
  • @Archemar thanks for your suggestion. I tried issuing the command without back quote but i still am facing the same issue.
    – ShivaPahwa
    Commented Feb 9, 2017 at 0:04

2 Answers 2


well, i am not a specialist myself but this may help,

when you use ssh command,you virtually enter the other machine "root@IP" as you mentioned

when you are in other machine,how can you execute your local file there,first you need to send your file there ,then only you can execute it there,

now try this

#scp file_you_need_to_send UserName@RemoteHost : Target_location_on_target_system this should copy your script on target system

#ssh UserName@RemoteHost to enter Target system

then exexute your respective script.


  • Thanks for the suggestion, I have the script in the remote machine not the local machine. I found a workaround for now. I dont create a cmd string anymore, i just run the command i.e. $(ssh root@IP "python /pathtoscript")
    – ShivaPahwa
    Commented Feb 10, 2017 at 1:16

ssh user@remotehost 'bash -s' < script.sh

from https://www.howtogeek.com/devops/how-to-run-a-local-shell-script-on-a-remote-ssh-server/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .