0

When I ssh into host and echo $PATH:

$ ssh my@host
$ echo $PATH

I get a different value for $PATH than when I run a script locally:

ssh my@host '\
    echo $PATH;'

Any idea why?

NOTE: It seems I don't get the full path variable when sshing from a script versus [other] CLI.

  • 1
    ssh opens login shell and what happens depends on the shell you use and your setup. For example if you use bash only /etc/profile is processed. – Arkadiusz Drabczyk Jul 19 '18 at 18:19
  • show us, don't make us guess. What was the output? – ctrl-alt-delor Jul 19 '18 at 18:56
0

This is due to the fact that when you run a command through ssh (ssh user@host 'command') it opens a non-login shell. An excellent breakdown of the differences between a login shell and a non-login shell can be found at this question. Essentially, what is causing you issue is that when you run the command through ssh your ~/.bash_profile is not sourced, meaning any modifications to the path it makes will not be available.

The solution is to either move these into your ~/.bashrc, which is sourced on opening a non-login shell, or as you found out, sourcing your .bash_profile directly in the script.

0

Adding this line to bash script worked

source ~/.bash_profile

source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.