0

I have a script on remote Debian server machine and I want to run a command by invoking:

ssh server "find.sh"

My find.sh script is in in:

/home/user/bin

First of all, I ran ssh server "echo $PATH" and it responded with this path:

/home/user/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

So it seems that PATH variable is ok but it doesn't work. Only way I could make it work was by invoking full path:

ssh server "/home/user/bin/find.sh"

Is there any simple way to make it work without specifying full path?

1
  • What is the permission of the find.sh file? It should be 755. If is only 644 it will not be executed automagically.
    – mdpc
    Commented Jun 10, 2015 at 0:17

3 Answers 3

3

You would need to define $PATH in your ~/.bashrc file on target server, at the beginning of the file :

export PATH=$PATH:/home/user/bin

https://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works :

Bash in non-interactive mode reads the file ~/.bashrc

2

ssh server "echo $PATH" is not the same mode of operation as ssh server "find.sh". The first contains shell metacharacters (specifically, a space) and therefore will be run by "sh -c 'specified command'"; the second does not, and therefore will be run by "exec('specified_command')". The difference is subtle, but in this case (because it is the shell which sets the environment variable) has side effects that you may not be expecting.

You can force a shell by doing something like ssh server 'bash -c "find.sh"'

2

Typing ssh server "echo $PATH" is going to expand PATH locally. So the command you are actually executing is ssh server "echo /home/user/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games", and the server will produce the same output every time it receives that command regardless of what PATH is on the server side. If you want to expand PATH on the server side you can use: ssh server 'echo "$PATH"' instead. Notice that single-quotes are being used to prevent local expansion of $PATH.

1
  • Good point, I forgot about expansion of "".
    – shadox
    Commented Jun 10, 2015 at 14:29

You must log in to answer this question.

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