2

I want to write a script that executes several commands on a remote server, which includes executing some applications. The .bashrc file on the remote machine defines the PATH variable so that these applications are under it.

But when I tried to use ssh <host> <command> , it seems that .bashrc was not loaded. ssh <host> 'echo $PATH' only show a few pathes like /usr/bin .

What confused me even more is that, even ssh <host> "source ~/.bashrc; <command>" not worked. ssh <host> 'source ~/.bashrc; echo $PATH' still only printed a few pathes. I checked by ssh <host> 'cat ~/.bashrc' and confirmed that this file does contains all the PATH definition, which was not affected in the environment where the command executed.

1
  • NB running ssh <host> "echo $PATH" would echo your path on the local system, not the remote system. Commented Sep 29, 2021 at 17:13

1 Answer 1

7

So after two hours of troubleshooting, I finally find the problem. It is because the following command in the beginning of .bashrc .

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

This prevents anything after that being executed in the environment of ssh <host> <command> .

If you have a similar issue try checking the beginning of the .bashrc file.

Sign up to request clarification or add additional context in comments.

2 Comments

This is one reason not to set environment variables in .bashrc. Put them in your login-shell configuration, and let any other non-login interactive shells inherit the variables.
This is what I'm doing now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.