Once upon a time, I needed smarter bash initialization scripts. Smarter in a lot of senses. Basically, something that could be handled more easily than a single .bashrc
file.
This is my current iteration.
Everything starts from the .bashrc
file, as usual. My current version just skips everything else in case it is not run in interactive mode.
Otherwise it passes the control (via source
built-in) to the actual initialization code, contained in the .rc
file.
This script is run within the very same context as the original (login) shell and loops through the files contained within a cofigurable directory, ${HOME}/.rc.d
by default.
All readable files are source
d in the order defined by the current (default) locale.
Unreadable files are simply skipped.
In my example I prefix file names with two digits number to make sure I have proper order of execution. Anything else can also be OK: YMMV.
As an example, I have added a few files with some stuff I normally setup for my bash
. Just a few notes.
- I have a fancy prompt setup. It basically is a plain old prompt withing narrow screens (<= 80 columns).
With wider screens it adds some interesting details: last command timestamp, optional git branch, optional non-trivial return code from previous command, userid and hostname, current path. - My locale is setup in my mother language (Italian) but I like to keep error messages in English and old-fashioned
ls
file sorting (ASCII-based). - I try to keep consistent time and date display format via my
TIMEFORMAT
variable.HISTTIMEFORMAT
(history timestamps) andTIME_STYLE
(ls
time/date output) are defined from there. - My own macro names all have a leading comma
,
(it can also be a colon:
) to avoid any command name conflict. Aliases instead have no comma to overrule the corresponding command. pathupdate
macro adds directories to thePATH
variables if they are not already there. It helps preventing its overgrowth.
- You can embedd the
.rc
file in the.bashrc
. You won't gain any noticeable init speedup at the possible cost of lesser flexibility. - Those are not script in the "usual" sense as they ar elacking the shebang prologue. This ensures all setup actions happen within the main shell.