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?

migrated from serverfault.com Jun 10 '15 at 17:22

This question came from our site for system and network administrators.

  • What is the permission of the find.sh file? It should be 755. If is only 644 it will not be executed automagically. – mdpc Jun 10 '15 at 0:17
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.

  • Good point, I forgot about expansion of "". – shadox Jun 10 '15 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.