9

I'd like to move my .bash_profile to Dropbox to have my custom Terminal functions available on all my Macs. Is this possible and are there possible implications?

For example I have a command for doing a git add and commit in one go:

function gax() {
    git add .
    git commit -m "$1"
}

I've been unable to find any information about this.

  • 2
    I know you've already got an answer, but it's got me thinking about just softlinking to the dropbox .bash_profile. Something to think about. – JMY1000 Jun 10 '16 at 6:39
  • @JMY1000 with my solution you can put additional stuff depending on the machine you're on - simply put the commands after the if statement. But You're right this could also be a symlink. – Mateusz Szlosek Jun 10 '16 at 9:00
  • 2
    ln -s ~/path/to/dropbox/bash_profile_file ~/.bash_profile – 2426021684 Jun 10 '16 at 16:11
18

You can have additional file in Dropbox folder and then on every machine inside your .bash_profile file put this:

FILE="/path/to/DropboxFolder/shared_bash_profile_file"
if [ -f $FILE ];
then
    source $FILE
fi
  • 5
    Since FILE could be used for other apps and such, you night want to name the variable something like "DROPBOX-RC" – grepsedawk Jun 10 '16 at 11:26
4

I think is better idea to upload to a repository. Let me tell you how I did it.

I version the whole ~/bin directory. My .bash_profile is on that directory. The $HOME/.bash_profile is a link to ~/bin/.bash_profile.

My .bash_profile looks like this:

if [[ $OSTYPE == darwin* ]]; then
    . ~/bin/includes/exports-osx.sh
    . ~/bin/includes/bash-stuff-osx.sh
    . ~/bin/includes/aliases-osx.sh
    . ~/bin/includes/functions-osx.sh
elif [[ $OSTYPE == linux* ]]; then
    . ~/bin/includes/exports-linux.sh
    . ~/bin/includes/terminal-linux.sh
    . ~/bin/includes/aliases-linux.sh
    . ~/bin/includes/ssh-keys-linux.sh
    . ~/bin/includes/bash-stuff-linux.sh
fi

. ~/bin/includes/bash-stuff.sh
. ~/bin/includes/aliases.sh
. ~/bin/includes/powerline.sh
. ~/bin/includes/functions.sh
. ~/bin/includes/work-stuff.sh

That way I can easilly track changes.

To keep the repo update you can create a cron or LaunchAgents script that pull the changes on your ~/bin directory once a day:

cd ~/bin && git pull origin $(git name-rev --name-only HEAD)
  • 2
    What advantages would this have over the simple solution that Mateusz presented? – swelet Jun 10 '16 at 8:26
  • 2
    @swelet from my point of view, two main advantages: you can easily revert changes to a point and you can keep track of every change. – jherran Jun 10 '16 at 8:28
  • @jherran Or you could put the git repo in the Dropbox folder as well. I do think though that Dropbox has some versioning itself. – pydsigner Jun 10 '16 at 16:27
  • 2
    @pydsigner I've had trouble with a Git repo in Dropbox. I'm guessing race conditions as Dropbox attempts to sync changes to a file that Git then attempts to reopen while it's still being used. – millimoose Jun 15 '16 at 10:59
  • This solution implicitly uses a public repo for a critical system file, which can lead to security issues. Dropbox and others are private. – New Alexandria Jul 4 '16 at 14:38
4

It is not a good idea to put the whole .bash_profile in Dropbox, either by tweaking the Dropbox paths or through a symlink. Different machines may require slightly different content of this file. Examples: different software versions installed requiring different configuration, different paths, different names for things such as partitions under /dev/.

Do this instead: put all your customized functions and aliases in a file $HOME/Dropbox/my_functions.sh and then include the line

. $HOME/Dropbox/my_functions.sh

in your .bash_profile.

You must log in to answer this question.

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