Bash – Including .bash_profile over ssh

bashrcssh

I am trying to write a script that will:

  1. Copy files to a server (I already have a script copy.sh that does this task)
  2. ssh to that server
  3. cd to the directory where the files I just copied are
  4. run make
  5. copy the resulting binary from make to another location.

My script looks like this:

#!/usr/bin/bash
BUILDSERV=me@server
BUILDDIR=/me/directory

#run my script that copies the files
./copy.sh

#TARGET is also a var in copy.sh so I make sure it's set properly here
TARGET=root@final_dest:/usr/bin/my_bin

ssh $BUILDSERV "cd $BUILDDIR && make && scp ./my_bin $TARGET"

The issue is that a program I want to run as part of make is not in my PATH. My .bash_profile has a export PATH=$PATH:/my/bin/ line, but it seems that bash_profile is not being read when I ssh in.

Is there a way to change my ssh invocation or script in general to make it read my .bash_profile?

Best Answer

The following should work:

ssh $BUILDSERV "source ~/.bash_profile && cd $BUILDDIR && make && scp ./my_bin $TARGET"

The source shell builtin reads a file and executed the commands in the same shell (unlike simply calling the script, which invokes a separate shell).

When invoked as login shell, bash executes the .bash_profile, if it exists, in exactly the same way as source does, therefore the effect will be the same.

Related Question