Different bash configurations for Linux vs OSX, interactive vs batch
(For related discussion see hackernews)
Are you tired of trying to remember what .bashrc
does vs .bash_profile
vs .profile
?
Are you tired of trying to remember how Darwin (Mac OS X) treats them differently from Linux?
Are you tired of not having your ~/.bash*
stuff work the way you expect?
Symlink all of the following files to bashrc_dispatch
(after reading warnings
below):
~/.bashrc
~/.bash_profile
~/.profile
(better to just get rid of it altogether)~/.bash_login
And then you can use these instead (in ~/.config/bash):
bashrc_once
: sourced at least once, and in most circumstances only once- before anything else.bashrc_all
: sourced on every bash instantiationbashrc_script
: sourced only when non-interactive / batchbashrc_interactive
: the one you'll probably fill up (mutually exclusive withbashrc_script
)bashrc_login
: sourced only when an interactive shell is also a login.
To reiterate,
bashrc_once
will be run before any of the others, but then won't be run again (ideally).bashrc_all
will run next, and will be run on every bash invocation. (so keep it light)- Then either
bashrc_script
orbashrc_interactive
will be run next depending on whether or not the bash invocation is interactive. - Finally, sometimes, like when you first ssh into a machine or often when
opening a new terminal window on a mac, the
bashrc_login
will be run after thebashrc_interactive
. Sobashrc_login
is the one where you'd echo a banner or whatever. For things like setting the PATH, use.bashrc_once
instead.
In addition to the dispatching, you'll forever have the following available:
$SHELL_PLATFORM
(eitherLINUX
,OSX
,BSD
,CYGWIN
orOTHER
),shell_is_linux
,shell_is_osx
,shell_is_bsd
,shell_is_cygwin
,shell_is_interactive
,shell_is_script
.
The functions are meant for clean conditionals in your new bashrc_*
scripts like:
$ shell_is_linux && echo 'leenux!'
or something like:
$ if shell_is_interactive ; then echo 'interact' ; fi
-
Obviously don't simply blow away your existing startup scripts if they have anything in them- you'll need their content to populate the new
bashrc_*
stuff. -
If you symlink
~/.profile
to this script you may be fine, but since it sometimes gets sourced by a truesh
command and the script currently has some bash-only stuff, it might not. Specifically, remove the symlink to~/.profile
if anything starts acting strange on startup or xwindows-based login. -
Be very careful what you put in
.bashrc_all
and.bashrc_script
- it may slow the system down. I put them there for conceptual completeness- that doesn't mean you have to use them (:
There are few knobs you can turn to make bashrc_dispatch
behave as you prefer.
-
EXPORT_FUNCTIONS
: set it tofalse
to disable the export of$SHELL_PLATFORM
and all theshell_is_*
functions and avoid polluting all the other shells' environments. -
Inside
bashrc_dispatch
you can changePRF=
to a location other than${HOME}/.
to have it look for your newbashrc_*
scripts somewhere else. -
If you find you need to modify your
bashrc_dispatch
script, please submit a patch if you like your modification.
- Joseph Wecker (initial author)
- Gioele Barabucci (fixes and optimizations)
- John Pell (fixes and optimizations)
Code : https://github.com/gaelicWizard/bashrc_dispatch
Report issues : https://github.com/gaelicWizard/bashrc_dispatch/issues
This is free software released into the public domain.