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.
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.
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.
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
).