I'd like to automate the process of syncing a local ~/bin folder to a remote host, and adding that folder to my remote shell's path. I've got the syncing part done, but am not sure how to automate adding the folder to my remote shell's $PATH.

This way I could always work on my shell scripts on my local machine, and if I have to connect to a new machine and start working on it, I could just run a local script to sync my ~/bin folder to the remote machine, and then run scripts on the remote host easily all the time.

So how can I ensure that my ~/bin path is added to the $PATH of the remote host, so the next time I login, the $PATH is set correctly?

I'd like this to be done automatically from my shell script.

EDIT: I'd prefer not to sync a .bashrc, but append to an existing file if need be by adding something to the end using a shell script.

  • Also sync an appropriate ~/.bashrc and such as well as ~/bin ?
    – thrig
    Commented Aug 26, 2015 at 16:04
  • Since adding to $PATH is a one-off it would probably be easier just to edit .bash_profile / .bashrc and have done with it. Commented Aug 26, 2015 at 16:04
  • I can do that, but I have to connect to lots of different machines all the time, and would like to automate the entire process, including the PATH addition.
    – Brad Parks
    Commented Aug 26, 2015 at 16:08
  • Add PATH="$HOME/bin:$PATH (or some such) to .bash_profile or .bashrc as part of your standard configuration. Once per host. Job done. It doesn't matter that $HOME/bin might not exist. Commented Aug 26, 2015 at 16:14

1 Answer 1


One approach that would work is just appending to the end of the bashrc rather than syncing it.

echo "PATH=\$PATH:~/bin" >> ~/.bashrc

This will add ~/bin onto the PATH variable.

In order to get this on a remote host you just need to call ssh first. You can use a for loop if you have lots of hosts.

for host in host1 host2 host3;do
    ssh user@$host echo "PATH=\$PATH:~/bin" >> ~/.bashrc
  • 1
    I ended up adding a grep around it to ensure it wasnt in the file already, and putting that in a script called path_add_bin_to_path, which is like the following: if ! grep -q PATH:~/bin ~/.bashrc; then echo "PATH=\$PATH:~/bin" >> ~/.bashrc fi I then called that script from my machine using ssh user@host ~/bin/path_add_bin_to_path, which I did in a script right after my syning operation, which is basically rsync -avrz --progress ~/bin/ -e ssh user@host:~/bin
    – Brad Parks
    Commented Aug 26, 2015 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .