Automatically updating your .bashrc when you log into a server

I am a long time bash user and have found numerous aliases and shell functions that allow me to be more productive at the prompt. Depending on how you manage (configuration management, NFS mounted home directories, etc.) ${HOME} making sure your bashrc gets updated when you find a cool new feature can be a pain. I was tinkering around last weekend and thought about adding a block of code to my bashrc to run curl to grab the latest version of my bashrc from github. The following short code block works for my needs:

# Location to pull bashrc from
bashrc_source="https://raw.githubusercontent.com/Matty9191/bashrc/master/bashrc"

# Take precaution when playing with temp files
temp_file=$(mktemp /tmp/tmp.XXXXXXXX)
touch ${temp_file}

curl -s -o ${temp_file} ${bashrc_source}
RC=$?

if [ ${RC} -eq 0 ]; then
    version=$(head -1 ${temp_file} | awk -F'=' '/VERSION/ {print $2}')

    if [ "${version}" -gt "${VERSION}" ]; then
        echo "Upgrading bashrc from version ${VERSION} to ${version}"
        cp ${HOME}/.bashrc ${HOME}/.bashrc.bak.$(/bin/date "+%m%d%Y.%S")
        mv ${temp_file} ${HOME}/.bashrc
    fi
else
    echo "Unable to retrive bashrc from ${bashrc_source}"
    rm ${temp_file}
fi

If a new version is available (a VERSION variable tracks the release #) I get the following output when I log in:

Upgrading bashrc from version 44 to 46

If github is unavailable due to a service issue or a firewall won’t let me out the script will let me know:

Unable to retrieve bashrc from https://gik/Matty9191/bashrc/master/bashrc

How are you keeping your shell profiles up to date? Let me know in the comment section.

2 thoughts on “Automatically updating your .bashrc when you log into a server”

  1. I find this a bit troubling from a security perspective.

    The url could point to anywhere if subjected to a MITM attack. Then you would be loading a custom script automatically, without ever knowing.

    I’d suggest storing a hash of the bashrc file on your phone, either in a password manager, or automatically as an SMS. And then let the script prompt you when there has been a change and that you acknowledge that change.

    Maybe I’m too paranoid, bored and depressed. But this troubled me a bit :)

Leave a Reply

Your email address will not be published. Required fields are marked *