I quite frequently will nano ~/.bashrc and add small little alias here and there on one of my boxes. The issue being, often I'll go to use this alias on another one of my PCs and it not be found. I could go and manually add the alias, or even attempt to replace the bashrc file with another, but that's too much work.

What I am looking for is a method wherein I log into a box, and it will download/update the bashrc file from some cloud medium as the latest version. I am unsure if a cron-job would work per-se as if I log into a bvox with an old file, and it attempt to sync it back to the cloud/online it may overwrite one with a more modern age.

How would one achieve this function?

migrated from askubuntu.com Apr 20 '18 at 13:32

This question came from our site for Ubuntu users and developers.

  • 1
    I'd be careful with this, as you have to make sure you ultimately trust the source you download from. After all, you're going to source that script without verifying it again, which is security critical. Anyway, I'd not sync the whole bashrc file but extract your aliases to a separate file called .bash_aliases which gets automatically sourced. That way you can separate it more easily. – Byte Commander Apr 20 '18 at 11:52
  • 1
    Note that ~/.bash_aliases is only automatically sourced on Ubuntu systems, so if any of the boxes you log into are not running Ubuntu, you'll still need to do it manually. Is there any reason you're not just using git for this? – terdon Apr 20 '18 at 12:16
  • @terdon No, there are no reasons; I just had this issue and wanted to know ways to re-mediate it. – ThisIsNotMyRealName Apr 20 '18 at 12:30
  • @ByteCommander If set boxes are all running Debian, would such method operate? – ThisIsNotMyRealName Apr 20 '18 at 12:30
  • 1
    No, but it is trivial to set up. It's just that Ubuntu ships with a line in .bashrc that sources .bash_aliases if it exists. You can copy it from your Ubuntu .bashrc. I'm moving this to Unix & Linux by the way since Debian is off topic Ask Ubuntu. – terdon Apr 20 '18 at 13:32

I would simply put all your alias definitions in a separate file, for example ~/.bash_aliases and keep this file on github. Then, add a line sourcing it to your ~/.bashrc:

if [ -e "~/.bash_aliases" ]; then
    . ~/.bash_aliases

Then, if the file has changed, fetch the latest from github and source again.

If that's not automated enough for you, you can instead keep the file somewhere you have http or ssh access to and add a line like this to your ~/.profile (not .bashrc so it isn't run every time you start a new shell):

scp user@server:~/.bash_aliases ~/

Or, for http:

wget http://example.com/.bash_aliases -O ~/.bash_aliases

That way, the file will be re-downloaded every time you log in, and then it will be sourced by your .bashrc file as explained above.

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.