write.as

Your `~/.bashrc` doesn't have to be a mess

If I had a cent for every time a README tells me to copy-paste some magic onto my ~/.bashrc, I suspect I would have amassed a sizeable fortune already.

I have adopted this solution a long time ago (more than 5 years, probably). Far from being an original idea, I think I stole it from some system daemon after looking at its respective /etc config dir. It also draws some inspiration from the Slackware init system.

I have this snippet in my ~/.bashrc:

BASHRC_D=~/.config/bashrc.d
[[ -r ${BASHRC_D}/bootstrap ]] && . ${BASHRC_D}/bootstrap

As you can see, the environment variable ${BASHRC_D} contains the path to a directory named ~/.config/bashrc.d. You are free to choose your own preferred location, of course.

On ${BASHRC_D}, I have a script named bootstrap, which is then sourced if it is readable. It contains the following snippet:

for file in ${BASHRC_D}/*.sh; do
  [[ -r $file ]] && . $file
done
unset file

In short, it iterates through every script with a .sh extension on ${BASHRC_D}. If it is readable, then it is sourced as well.

I have just added a new script to ${BASHRC_D} after installing .NET Core on Debian 10. I was informed that

The .NET Core tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Well, thanks for letting me know. The variable name is pretty descriptive but I don't want to pollute my pristine ~/.bashrc with it. So all I had to do was to execute the following commands:

~$ echo 'export DOTNET_CLI_TELEMETRY_OPTOUT=1' > "${BASHRC_D}/dotnet_cli_telemetry_optout.sh"
~$ exec bash
~$ echo $DOTNET_CLI_TELEMETRY_OPTOUT
1

Also, there is a big advantage to this layout. What if I want to disable one of these configuration snippets temporarily? There are two options, based on the behavior of the bootstrap script: