0

I'm having trouble executing some commands over ssh in localhost, I'm using zsh on mac:

% echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Library/TeX/texbin:/usr/local/munki:/opt/X11/bin:/opt/cisco/anyconnect/bin

% ssh localhost echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Library/TeX/texbin:/usr/local/munki:/opt/X11/bin:/opt/cisco/anyconnect/bin

% which docker
/usr/local/bin/docker

% ssh localhost which docker
docker not found

I don't know what could be wrong in here, tried ssh -t but with the same results.

EDIT:

Thanks for the comments, it is actually different, why is that?

% ssh localhost printenv PATH
/usr/bin:/bin:/usr/sbin:/sbin
4
  • 3
    your 2nd command confuses you. The $PATH is expanded before ssh is executed. Instead, do ssh localhost 'echo $PATH'.
    – waltinator
    Apr 16 '20 at 14:45
  • 1
    Please show in your question the results when you use printenv PATH instead of echo $PATH in both cases.
    – JdeBP
    Apr 16 '20 at 14:45
  • without ssh shows the same path, ssh version added.
    – vlizana
    Apr 16 '20 at 16:00
  • Found an explanation and solution here, maybe its a duplicate?
    – vlizana
    Apr 16 '20 at 20:15
2

Variable interpretation is done on the command line before running any code. You could send the variable to the SSH server by escaping it or single-quoting it, e.g.

% echo $HOST
tabasco
% ssh mesquite echo $HOST
tabasco
% ssh mesquite "echo $HOST"
tabasco
% ssh mesquite 'echo $HOST'
mesquite
% ssh mesquite echo \$HOST
mesquite
% ssh mesquite "echo \$HOST"
mesquite
% ssh mesquite 'echo \$HOST'
$​HOST
% printenv HOST
tabasco
% ssh mesquite printenv HOST
mesquite

(Note, not all hosts actually have a $HOST variable by default. BSD, for example.)

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.