Assuming user has /bin/bash
as the shell in /etc/passwd
. Then ssh user@host command
runs the command using Bash. However, that shell is neither login nor interactive, which means neither ~/.bash_profile
nor ~/.bashrc
is sourced. In that case how to set the PATH
environment variable so that executables can be found and executed? Is it recommended to prefix the actual command with source ~/.bashrc
?
Edit. This question is trivial for Bash, because (as people pointed out) ~/.bashrc
is sourced in such case. The definitive answer comes from this paragraph in man bash
:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If
bash
determines it is being run in this fashion, it reads and executes commands from~/.bashrc
, if that file exists and is readable. It will not do this if invoked assh
. The--norc
option may be used to inhibit this behavior, and the--rcfile
option may be used to force another file to be read, but neither rshd nor sshd generally invoke the shell with those options or allow them to be specified.
.bashrc
is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use~/.pam_environment
.