0

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 Aug 26 '15 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. – roaima Aug 26 '15 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 Aug 26 '15 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. – roaima Aug 26 '15 at 16:14
2

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
done
  • 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 Aug 26 '15 at 17:51

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.