Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from serverfault.com Jun 10 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 at 0:17

3 Answers 3

up vote 3 down vote accepted

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

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

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

share|improve this answer

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.

share|improve this answer
    
Good point, I forgot about expansion of "". –  inshador Jun 10 at 14:29

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"'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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