Skip to content

Instantly share code, notes, and snippets.

@x3rAx
Last active April 27, 2024 12:47
Show Gist options
  • Save x3rAx/72bd34ed1ecb0587b13092cabed4d2e6 to your computer and use it in GitHub Desktop.
Save x3rAx/72bd34ed1ecb0587b13092cabed4d2e6 to your computer and use it in GitHub Desktop.
Setup instructions for `~/.shellrc.d/`

Shellrc.d

Setup

Create the directory ~/.shellrc.d

mkdir ~/.shellrc.d

Move the shell's rc files to ~/.shellrc.d

mv ~/.bashrc ~/.shellrc.d/00--bashrc.bash
mv ~/.zshrc ~/.shellrc.d/00--zshrc.zsh

Create new shellrc files (eg. ~/.bashrc or ~/.zshrc):

# .bashrc
# =======

# --- START of .shellrc.d ---
# See: https://gist.github.com/x3rAx/72bd34ed1ecb0587b13092cabed4d2e6

    # Replace this with the shell specific file suffix
    # (eg. `bash` for `.bash` files or `zsh` for `.zsh` files)
    __SHELL_SUFFIX='bash'

    _IFS=$IFS
    IFS=$'\n'
    # For POSIX incompatible shells, remove this part ----------------------------------------------------------vvvvvvvvvvvvvvvv
    for f in $(find ~/.shellrc.d -type f \! -name '#*' -and \! -path '*/#*' -and \( -name "*.${__SHELL_SUFFIX}" -or -name '*.sh' \) | sort ); do
        source "$f"
    done

    IFS=$_IFS
    unset _IFS f __SHELL_SUFFIX

# --- END of .shellrc.d ---

For POSIX incompatible shells like fish, translate the above file to the respective shell syntax and remove the -or -name '*.sh' part from the find command so that only shell specific files are used.

Add shellrc files

General shellrc files

Place general files that should be sourced by all POSIX compatible shells in ~/.shellrc.d with suffix .sh (eg. ~/.shellrc.d/path.sh). These files should be POSIX compatible. Use shellcheck -s sh <filename> to check if it is. You can also check all .sh files in ~/.shellrc.d with the command:

shellcheck -s sh ~/.shellrc.d/*.sh

See an example for path.sh below.

Specific shellrc files

Put shell specific files in ~/.shellrc.d/ with the respective shell suffix (eg. .bash for bash) that you configured with __SHELL_SUFIX in your respective shellrc file.

See an example for pureline.bash below.

Disable shellrc files

To disable a file, just prefix its filename with # (eg. ~/.shellrc.d/#my_ps1.bash). Directories prefixed with # are also ignored (eg. ~/.shellrc.d/#disabled/my_ps1.bash).

# path.sh
# =======
__remove_from_path() {
PATH="$(echo ":$PATH:" | sed -e "s:\:${1}\::\::g" -e 's:^\:\(.*\)\:$:\1:g')"
}
__prepend_path() {
if [ ! -d "$1" ]; then return; fi
__remove_from_path "$1"
PATH="${1}:${PATH}"
}
__append_path() {
if [ ! -d "$1" ]; then return; fi
__remove_from_path "$1"
PATH="${PATH}:${1}"
}
__prepend_path "${HOME}/.local/bin"
# Unset the functions
unset -f __remove_from_path __prepend_path __append_path
# pureline.bash
# =============
source "${HOME}/.local/git/pureline/pureline" "${BASH_SOURCE%/*}/pureline.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment