tdobek
tdobek

Reputation: 1176

ssh executing commands remotely, how PATH is determined?

I have a bash script that uses ssh to execute commands remotely. I spotted however quite considerable difference in behaviour depending whether I launch commands in the background or not:

ssh host cmd1 && cmd2 && cmd3

vs.

ssh host "cmd1 && cmd2 && cmd3" &

In the second case, some commands are not executed correctly because they don't appear on $PATH. It seems the environment is different in those two situations (when passing env as command I noticed quite considerable differences).

How to explain that ?

I know I could override PATH variable but is there a way to do it more in a elegant way to have the second example behave like first with "&" feature ?

Upvotes: 0

Views: 1096

Answers (2)

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44142

In the first command you are probably executing cmd2 and cmd3 on the local host, not the remote one. The && words are interpreted by the local shell and not passed as arguments to ssh.

Upvotes: 6

ismail
ismail

Reputation: 47662

PATH is set while compiling sshd:

[~]> grep PATH /etc/sshd_config         
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

Always use absolute paths while executing a remote command.

Upvotes: 1

Related Questions