In my last blog, Getting Started with Bash Shell, I gave an introduction to the Bash Shell. I went over what a shell is, how to get started with Bash and provided some simple examples of the power of the Bash Shell.
3minutes remaining
Now, I’m going to explain how to customize and organize Bash to increase productivity and get more out of your Bash experience.
There are many ways you can setup and configure your shell; this demonstrates how I do it and what works for me. If you're new to this it should provide a useful starting point, and if you're more experienced this should be familiar. Either way, it's fairly straightforward and hopefully you learn some useful tips.
Let's Jump In!
To get started, we will create the folder and files used to organize our Bash configuration and customization. I sync my Bash configuration across multiple computers, but for now I'll keep it simple and skip that step. Make sure you're in your home directory (cd) and run the following two commands:
mkdir .bash touch .bash/{aliases,config,env}
Commands can be chained together with a semicolon, so if you wanted to do this on one line you would run:
mkdir .bash; touch .bash/{aliases,config,env}
So what did we just do? The first command creates a hidden folder named .bash, the second command creates three empty files inside the hidden .bash directory.
Ok, Now What?
Ok, great, we've created a hidden folder and three empty files, big deal.
Let's step back and take a look at what happens when an interactive shell is launched. When a Bash Shell login session is started the ~/.bash_profile file is read and commands in this file are executed. Depending on your operation system ~/.bashrc may be run an executed.
Next check if the .bash_profile and .bashrc files exist in your home directory. If these files don't exist you can create them using: touch ~/{.bash_profile,.bashrc}
Next, review the contents of each file. If you just created them they will be empty. There are a number of ways to read each file, we'll use the cat command to peak at each file.
cat ~/.bash_profile cat ~/.bashrc
.bash_profile
What should be inside ~/.bash_profile? The .bash_profile file should contain anything you want run when you first log into an interactive Bash Shell. Add the following lines to your .bash_profile:
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
This block of code checks to make sure the .bashrc file exists and, if it does, executes the commands inside the file. I include this in my .bash_profile because I'm running OS X and while some unix systems will read and execute your .bashrc file OS X does not.
To edit your .bash_profile and add the code above run:
nano ~/.bash_profile
nano is a command line text editor. If you're not familiar with it review the GNU nano page on Wikipedia or use the built in man pages (man nano).
.bashrc
Your .bashrc contains everything you want to run and execute during an interactive Bash session. Examples include aliases you use, environment settings and any configurations you want. While you could have all these in the .bashrc file, I split these into the three files we created at the beginning. Edit your .bashrc file (nano ~/.bashrc) and add the following lines:
Make sure you include the . at the beginning of each line. The . is a shortcut for the source command we used earlier.
Example Settings
This next sections provide some example settings for what I include in each of these files.
aliases
I create aliases for commands or tasks that I do often or for long, complex command that may be useful in the future.
I have the following aliases for editing my configuration files in sublime. (Note: The open command is OS X specific)
alias edit-bash-aliases="open -a 'Sublime Text' ~/.bash/aliases" alias edit-bash-config="open -a 'Sublime Text' ~/.bash/config" alias edit-bash-env="open -a 'Sublime Text' ~/.bash/env" alias edit-hosts="open -a 'Sublime Text' /private/etc/hosts" alias edit-apache-config="open -a 'Sublime Text' /etc/apache2/httpd.conf"
I also setup aliases to view my configuration files:
alias view-bash-aliases='cat ~/.bash/aliases' alias view-bash-config='cat ~/.bash/config' alias view-bash-env='cat ~/.bash/env'
Aliases support auto-complete and are easy to search. I name and group my aliases to take advantage of auto-complete, for example if I type edit and hit the tab key twice I see all of my edit aliases (figure 1).
Figure 1
If I want to search for aliases configured with Sublime I'll use:
alias | grep Sublime
Figure 2 shows the search results when I search my aliases for Sublime.
Figure 2
env
In my env file I set my bash history settings, my default editor, my PATH variable and other global settings. Here are some of my custom settings:
export EDITOR="nano" export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin export HISTSIZE=10000 # Default is 500 export HISTFILESIZE=1000000 export HISTTIMEFORMAT='%b %d %I:%M %p ' # using strftime format export HISTIGNORE="history:pwd:exit:df:ls:ls -la:ll" export GREP_COLOR="33;40" export GREP_OPTIONS="--color=auto"
config
We use Git for version control on many of the projects we work on at Imarc. For added Git support in my Bash Shell I use git-completion.bash to auto complete Git commands and git-prompt.sh to customize my command prompt. To load these files I have the following code in my ~/.bash/config file:
if [ -f ~/.git-completion.bash ]; then source ~/.git-completion.bash; fi
if [ -f ~/.git-prompt.sh ]; then source ~/.git-prompt.sh; fi
These commands check to see if the files .git-completion.bash and .git-prompt.sh exist in my home directory. If the file exists the contents of the file are executed.
Syncing
As I mentioned earlier I sync my Bash settings across multiple computers. There are many tools and cloud servers available for file synchronization, I use Dropbox. Once Dropbox is installed all I have to is create a symbolic link to the Dropbox location of my .bash folder. Figure 3 shows my .bash files and symbolic links in my home directory.
Figure 3
As you can see my .bash folder is located in /Users/Mat/Dropbox/Sync/Bash/ not /Users/Mat. To create a new symbolic link use the ln command with the -s flag, then specify the source (the dropbox directory) and the target (your home directory). To move the .bash directory we create earlier and create a symbolic link run the following two commands from our home directory. Make sure you replace username with your user name. These commands also assumes Dropbox is installed in /Users/username/ if it is not, update the path to where you installed Dropbox.
The first command moves the directory to our preferred dropbox location (for this example I'm just moving it to the dropbox root, you can put it anywhere you want). The second command creates a link in our current directory to the location of our .bash folder. Note the . in the second command, this specifies the current directory.
That's all for now
You should now have enough information to get started customizing and organizing your own Bash environment. I'm always tinkering with my settings; play around and see what works best for you.