Skip to content

Instantly share code, notes, and snippets.

@bcomnes
Last active October 6, 2021 15:38
Show Gist options
  • Save bcomnes/5053fca2d7be573c0abd to your computer and use it in GitHub Desktop.
Save bcomnes/5053fca2d7be573c0abd to your computer and use it in GitHub Desktop.
bashrc utopia
#
# ~/.bash_profile
#
# Do everything in bashrc
[[ -f ~/.bashrc ]] && . ~/.bashrc
#
# ~/.bashrc
#
# .d folder style bashrc
# https://gist.github.com/bcomnes/5053fca2d7be573c0abd
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
run_scripts () {
local script
for script in $1/*; do
# skip non-executable snippets and folders
[ -f "$script" ] && [ -x "$script" ] || continue
# execute $script in the context of the current shell
. $script
done
}
# if DEFAULT_PATH not set
if [[ -z "$DEFAULT_PATH" ]] ; then
# set it!
export DEFAULT_PATH=$PATH
fi
# reset path to default path for idempotent bashrc sourcing
export PATH=$DEFAULT_PATH
# bashrc folder
BASHRCD=~/.bashrc.d
# run all the bash settings
run_scripts "$BASHRCD"
@bcomnes
Copy link
Author

bcomnes commented Dec 21, 2015

Load order can be further enforced with init.d style number prefixes:

00_rc_snippet.sh # runs before...
01_rc_snippet.sh # ...
20_rc_snippet.sh # etc

@bcomnes
Copy link
Author

bcomnes commented Dec 28, 2015

You can also call this same pattern for subfolders e.g. .bashrc.d/darwin, .bashrc.d/linux etc.

#!/bin/bash

# `.bashrc.d/platformrc.sh`

# https://gist.github.com/bcomnes/13711d12237e866de5ca
if [ "$(uname)" == "Darwin" ]; then
    # Do something under Mac OS X platform
    run_scripts ./darwin
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    # Do something under GNU/Linux platform
    run_scripts ./linux
    echo "loading linux"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
    # Do something under Windows NT platform
    run_scripts ./mingw32_nt
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment