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
vlizana
  • 137

1 Answers1

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.)

Adam Katz
  • 3,965