├── README.md └── bashrc_dispatch /README.md: -------------------------------------------------------------------------------- 1 | bashrc_dispatch: Different bash configurations for Linux vs OSX, interactive vs batch 2 | ===================================================================================== 3 | 4 | Are you tired of trying to remember what `.bashrc` does vs `.bash_profile` 5 | vs `.profile`? 6 | 7 | Are you tired of trying to remember how Darwin (Mac OS X) treats them 8 | differently from Linux? 9 | 10 | Are you tired of not having your `~/.bash*` stuff work the way you expect? 11 | 12 | Symlink all of the following files to `bashrc_dispatch`: 13 | 14 | * `~/.bashrc` 15 | * `~/.bash_profile` 16 | * `~/.profile` 17 | * `~/.bash_login` 18 | 19 | And then you can use these instead: 20 | 21 | * `~/.bashrc_all`: sourced on every bash instantiation; 22 | * `~/.bashrc_script`: sourced only when non-interactive; 23 | * `~/.bashrc_interactive`: the one you'll probably fill up (*mutually 24 | exclusive* with `~/.bashrc_script`); 25 | * `~/.bashrc_login`: sourced only when an interactive shell is also a login. 26 | 27 | To reiterate, 28 | 29 | 1. `~/.bashrc_all` will always be run first. 30 | 2. Then either `~/.bashrc_script` *or* `~/.bashrc_interactive` will be run 31 | next depending on whether or not the bash invocation is interactive. 32 | 3. Finally, sometimes, like when you first ssh into a machine or often when 33 | opening a new terminal window on a mac, the `~/.bashrc_login` will be run 34 | after the `~/.bash_interactive`. So `~/.bashrc_login` is the one where 35 | you'd echo a banner or whatever. 36 | 37 | In addition to the dispatching, you'll forever have the following available: 38 | 39 | * `$SHELL_PLATFORM` (either `LINUX`, `OSX`, `BSD` or `OTHER`), 40 | * `shell_is_linux`, 41 | * `shell_is_osx`, 42 | * `shell_is_interactive`, 43 | * `shell_is_script`. 44 | 45 | The functions are meant for clean conditionals in your new `~/.bashrc_*` 46 | scripts like: 47 | 48 | $ shell_is_linux && echo 'leenux!' 49 | 50 | or something like: 51 | 52 | $ if shell_is_interactive ; then echo 'interact' ; fi 53 | 54 | And now I think these comments have reached parity with the code itself which 55 | should be easy to extend. 56 | 57 | 58 | Configuration 59 | ------------- 60 | 61 | There are few knobs you can turn to make `bashrc_dispatch` behave as you prefer. 62 | 63 | * `EXPORT_FUNCTIONS`: set it to `false` to disable the export of 64 | `$SHELL_PLATFORM` and all the `shell_is_*` functions and avoid polluting all 65 | the other shells' environments. 66 | 67 | Authors 68 | ------- 69 | 70 | * Joseph Wecker (initial author) 71 | * Gioele Barabucci (fixes and optimizations) 72 | 73 | 74 | Development 75 | ----------- 76 | 77 | Code 78 | : 79 | 80 | Report issues 81 | : 82 | 83 | 84 | License 85 | ------- 86 | 87 | This is free software released into the public domain. 88 | 89 | -------------------------------------------------------------------------------- /bashrc_dispatch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Launch different bash configuration for Linux vs OSX, interactive vs batch 4 | # 5 | # More info at https://github.com/gioele/bashrc_dispatch 6 | # 7 | # License: Public Domain. 8 | # Author: Joseph Wecker, 2012 9 | 10 | # Configuration 11 | # ------------- 12 | # 13 | # EXPORT_FUNCTIONS: export SHELL_PLATFORM and shell_is_* functions for use 14 | # in other scripts. 15 | 16 | EXPORT_FUNCTIONS=true 17 | 18 | 19 | # Code 20 | # ---- 21 | 22 | # Avoid recursive invocation 23 | 24 | [ -n "$BASHRC_DISPATCH_PID" ] && [ $$ -eq "$BASHRC_DISPATCH_PID" ] && exit 25 | BASHRC_DISPATCH_PID=$$ 26 | 27 | 28 | # Setup the main shell variables and functions 29 | 30 | SHELL_PLATFORM='OTHER' 31 | case "$OSTYPE" in 32 | *'linux'* ) SHELL_PLATFORM='LINUX' ;; 33 | *'darwin'* ) SHELL_PLATFORM='OSX' ;; 34 | *'freebsd'* ) SHELL_PLATFORM='BSD' ;; 35 | esac 36 | 37 | if ! type -p shell_is_login ; then 38 | shell_is_linux () { [[ "$OSTYPE" == *'linux'* ]] ; } 39 | shell_is_osx () { [[ "$OSTYPE" == *'darwin'* ]] ; } 40 | shell_is_login () { shopt -q login_shell ; } 41 | shell_is_interactive () { test -n "$PS1" ; } 42 | shell_is_script () { ! shell_is_interactive ; } 43 | fi 44 | 45 | 46 | # Make $BASH_ENV the same in interactive and non-interactive scripts 47 | 48 | [ -z "$BASH_ENV" ] && export BASH_ENV="$BASH_SOURCE" 49 | 50 | 51 | # Now dispatch special files 52 | 53 | [ -f "${HOME}/.bashrc_all" ] && . "${HOME}/.bashrc_all" 54 | [ -f "${HOME}/.bashrc_script" ] && shell_is_script && . "${HOME}/.bashrc_script" 55 | [ -f "${HOME}/.bashrc_interactive" ] && shell_is_interactive && . "${HOME}/.bashrc_interactive" 56 | [ -f "${HOME}/.bashrc_login" ] && shell_is_login && . "${HOME}/.bashrc_login" 57 | 58 | 59 | # Export or unset functions and shell variables 60 | 61 | if $EXPORT_FUNCTIONS ; then 62 | fn_cmd='export' 63 | else 64 | fn_cmd='unset' 65 | fi 66 | 67 | $fn_cmd SHELL_PLATFORM 68 | $fn_cmd -f shell_is_linux 69 | $fn_cmd -f shell_is_osx 70 | $fn_cmd -f shell_is_login 71 | $fn_cmd -f shell_is_interactive 72 | $fn_cmd -f shell_is_script 73 | 74 | 75 | # Unset local variables 76 | 77 | unset fn_cmd 78 | 79 | unset EXPORT_FUNCTIONS 80 | unset BASHRC_DISPATCH_PID 81 | --------------------------------------------------------------------------------