I have the following in my .emacs:
(require 'tramp)
(add-to-list 'tramp-remote-path "/some/path")
(add-to-list 'tramp-remote-path 'tramp-default-remote-path)
so when I open a file using tramp over ssh, I expect my PATH
to contain /some/path
. Instead, running M-! echo $PATH
returns
/bin:/usr/bin:/usr/sbin:/usr/local/bin
Even if I set export PATH=/hwdisks/data/modules/pkg/git/1.8.4.1/bin/git:$PATH
in .bashrc
or .profile
, PATH
is not set correctly.
In the tramp log, *debug tramp/ssh remotehost*
, I can see tramp explicitly setting PATH
:
12:28:34.202135 tramp-send-command (6) # PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin; export PATH
If I include echo "in .bashrc"
in my .bashrc
, it appears in the tramp log, so I know tramp is reading it.
How can I make tramp use the correct PATH
?
Emacs version: 24.2.1
Tramp version: 2.2.3-24.1
Embarrassingly, the answer is in the tramp manual:
Another possibility is to reuse the path settings of your remote account when you log in. Usually, these settings are overwritten, because they might not be useful for tramp. The place holder tramp-own-remote-path preserves these settings. You can activate it via
(add-to-list 'tramp-remote-path 'tramp-own-remote-path)
I'm still not sure why it ignores the other paths added to tramp-remote-path
.
I read this in tramp manual and tried it. Added
(add-to-list 'tramp-remote-path 'tramp-own-remote-path)
Then exited emacs, removed ~/.emacs.d/tramp
, and restarted as explained here
But M-! echo $PATH
still shows the value of tramp-default-remote-path
instead of the value set in my .bashrc
.
The issue looks to be my understanding of how profiles are loaded. The way tramp calls the remote shell to get the remote PATH:
/bin/sh -l -c 'echo $PATH'
/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin
Since bash is call as login shell and as /bin/sh
it reads .profile
instead of .bash_profile
or .bashrc
.
I found this in bash manual page:
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.
I usually configure only ~/.bashrc
and ~/.bash_profile
For tramp to work properly looks like I should move my .bash_profile
to .profile
Hope this helps others.
- GNU Emacs: 25.0.94.2 Tramp: 2.2.13.25.1
来源:https://stackoverflow.com/questions/26630640/tramp-ignores-tramp-remote-path