1

I have two laptops--one for work, one for personal stuff. My .bash_profile on each has a lot of the same functions and aliases. However, there is some stuff on my work laptop's .bash_profile that doesn't exist on my personal laptop's.

I'm wondering if there is a way I can make a "shared" .bash_profile or something, and extend functionality to it so it's easier to share my bash stuff between laptops.

I'm using a Mac if that matters; bash --version == 4.4.19

Nxt3
  • 1,970
  • 4
  • 30
  • 52

3 Answers3

4

Sure it's possible, and not that uncommon to break up a .bash_profile into modules using the source command.

From your question it sounds like your .bash_profiles are almost identical, but the only difference is that you have work specific stuff on your work laptop. One way you could break it up would be to move your work aliases, functions, exports, etc. to their own file. We'll call it ~/.work_profile

Then in your ~/.bash_profile add the following. [[ -f ~/.work_profile ]] && source ~/.work_profile

An explanation: [[ -f ~/.work_profile ]] tests that the regular file ~/.work_profile exists and will return true if it does.

&& source ~/.work_profile is a logical and. If the previous command returns true, then it will run this part and source your ~/.work_profile.

Jack Bracken
  • 1,233
  • 12
  • 23
1

Solution 1:

  1. Put common stuff in the shared .bash_profile
  2. source ~/.bashrc.local at the end of .bash_profile
  3. Each laptop has its own .bashrc.local.

Solution 2: Use one single .bash_profile on both laptops.

... common stuff ...

if [[ -f /a/file/only/on/work/laptop ]]; then
  ... now on work laptop ...
else
  ... now on home laptop ...
fi

I have 20+ Bash rc files so my real solution is a bit more complicated:

  1. There's only one line in ~/.bashrc_profile:

    source ~/.bashrc
    
  2. All systems share the same ~/.bashrc in which the logic is like this:

    # Each system has its own `.bashrc.pre' which defines the
    # var `$RC_DIR' for top dir of all rc files (Bash, Vim, ...).
    #
    # I need to define my own `$RC_DIR' because my coworkers share
    # some servers (using the same accounts) at work.
    source ~/.bashrc.pre
    
    # `rcfiles' is system specific which lists rc files to be
    # loaded on that system. I maintain a `rcfiles.template'.
    for file in $(< $RC_DIR/bash/rcfiles); do
        source $file
    done
    
    # `.bashrc.post' is system specific
    source ~/.bashrc.post
    
pynexj
  • 19,215
  • 5
  • 38
  • 56
0

You can either use the profile file in /etc if you have root, or use source filename to include other (common) file.

You may also want to have a look at this question explaining the different loadable files.

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62