Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to run a command remotely with SSH, but my problem is that when I do that, my .basrhc doesn't get executed, and so the PATH is all wrong and my script is breaking.

What I'm doing is

ssh 'my_command'

I've also tried things like

ssh 'source ~/.bashrc; my_command' and similar variants, but nothing seems to work.

share|improve this question

SSH with a single, simple command argument will not start a shell, which means .bashrc does not get executed.

SSH with a compound command argument such as your second example should start a shell, but it will be a non-interactive one, so that won't run .bashrc by default. See Bash Startup Files in the Bash manual for more information.

However, calling 'source' in your second example command should work. Does your .bashrc have a line like if [ "$PS1" ]; then or if [ -z "$PS1" ]; then at the top? The sample bashrc does have one of these lines, and it prevents the script commands from running unless you are on an interactive shell. As mentioned above, SSH does not start an interactive shell by default when you pass a command argument. Try ssh -t 'source ~/.bashrc; my_command', as this will force TTY allocation and act like an interactive shell.

share|improve this answer

Zancheys Post gives you the right solution.

An other aproach could be to provide a simple shell script like following.

# program name: /usr/local/bin/my_command_ssh
export PATH=/home/custom/bin:/usr/local/bin:/usr/bin:/bin

And now you can call it via ssh:

ssh -p22 '/usr/local/bin/my_command_ssh'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.