Friday, March 19, 2010

Bash initialization

Preface: I am using darwin (OS X) but most of the following should apply to other unix systems. For a good similar description written for linux users, please see this. I'm not a shell master but after spending a lot of time in man pages yesterday, I found out a lot that didn't show up in my quick web searches. Also, since I was mostly interested in variable and function bindings, I don't mention any other common customizations like shell options, .inputrc, and completion.

So you want to customize your bash shell using some initialization scripts. How should you do it? The man page is well written and you can learn a lot especially from the "INVOCATION" section. But it's also complicated and there are a number of choices without much direction about which method to use for each purpose. Here are some of your options:

  • Edit ~/.bash_profile
  • Edit ~/.bashrc
  • Edit ~/.profile
  • Define the BASH_ENV variable
  • Use the --rcfile command line option
Since I'm focussing on the definition of variables and functions, it's important to start with a rough understanding of how environments work. So here's a quick description. When you define a variable or a function without exporting the definition, the binding is only visible to the shell that created the binding. Subshells and other processes spawned by the shell only inherit definitions that are exported. So you can generally decide which initialization file to use for each binding once you know your answer to the question, "Which programs should be able to see this binding?".

The simplest policy is to just let all your programs see all the bindings. You can achieve this by putting all your bindings in ~/.bash_profile and exporting them all.

You rarely want to define something in ~/.bash_profile without exporting it because when you do that, interactive subshells will not see the binding and you will probably be confused when you spawn a subshell from your editor and find that your usual shell environment is missing stuff. Some variables like PATH come already exported and you don't have to export them yourself. But new bindings defined in ~/.bash_profile should almost certainly be exported.

If you want a binding to be available in all interactive shells but you don't want it inherited by other processes, then you want to put the binding into ~/.bashrc without exporting it.

If you have some bindings that only make sense in the context of a particular project and you want them defined only for interactive shells you use to work on that project, then you probably want to go with --rcfile. Specify some project-specific bashrc file other than ~/.bashrc and probably source ~/.bashrc from within that file. This method is also good for adding a directory onto your PATH so you can run your project specific scripts easily from such shells without polluting the global namespace.

The BASH_ENV variable is only relevant to non-interactive shells like those running your #!/bin/bash scripts. These shells do not source ~/.bashrc. So if you want some bindings available to your non-interactive shells within some context, then you can assign the name of a file to BASH_ENV and put your bindings in that file. The variable is expanded by the shell so you can play some tricks with that too if you are so inclined.

The ~/.profile file is only useful if you sometimes use the plain old sh shell. If you do plan to use sh, then you can put definitions into ~/.profile and sh login shells will see them. You can source ~/.profile in your ~/.bash_profile to make sure the definitions are also available to bash.

There are other things to use initialization scripts for but you can get a long way with just some custom variable and function bindings. If you were scratching your head about where to put them, hopefully this post has helped.

Here's a simple example based on my own setup on my darwin (OS X) machine:

~/.profile:
PATH=/opt/local/bin:$PATH:~/Unix/bin

~/.bash_profile:
# No need to source /env/profile or /env/bashrc
# they are sourced automatically.
# Must source ~/.profile though or it will be
# ignored.
. ~/.profile
. ~/.bashrc

~/.bashrc:
cl() {
cd $1
ls
}
# etc etc

# When I want to work on myproject, I will use a
# customized shell. To start working on myproject,
# I do something like this:
# $ bash --rcfile ~/Unix/myproject/bashrc

~/Unix/myproject/bashrc:
. ~/.bashrc
. ~/Unix/myproject/env.bash
PATH=$SCRIPTS:$PATH
cd $TOP

~/Unix/myproject/env.bash:
TOP=~/Unix/myproject
SCRIPTS=$TOP/scripts
# BASH_ENV only works if it is exported.
export BASH_ENV=$TOP/env.bash

~/Unix/myproject/scripts/clean:
#!/bin/bash
# This script is non-interactive.
# The TOP variable is defined due to BASH_ENV.
[[ -n $TOP ]] && rm -rf $TOP/cache/*