Make the Linux or Mac OS X Command Line Easier with .bashrc

(2 votes, average 5.00 out of 5)

Starting to use the command line (or terminal) in Linux or Mac OS X can be very daunting. It is indeed a complex task to learn all of the commands and how to use them together properly. Despite the learning curve, using the command line can make a lot of tasks significantly faster and easier. One way to make the terminal easier to use is by creating a .bashrc file.

Most Linux distributions (and Mac OS X) use the Bash Shell as the default terminal shell (although I've heard some great things about zsh). When initializing, Bash reads from a file called .bashrc (in your home directory, aka ~/.bashrc). You can add custom commands and shortcuts to this file so that you don't need to remember complex commands and personalize the shell. After making changes to .bashrc, make sure to run source ~/.bashrc to reload the file into the open shell. Here are a few things you can do with .bashrc:

Aliases

An alias is a way to create a new command. You can use an alias to help simplify repetitive or complex commands. For instance, in Debian-based Linux distros, the typical way to install a package is to use sudo apt-get install package_name. This can get tedious if you often install packages. To make things simpler, try adding this alias to your .bashrc:

1
alias install="sudo apt-get install $@"

Now, installing a package is as simple as install package_name. The $@ part of the alias is a special variable which holds all of the command arguments. For example, running install shotwell wine1.2 pidgin will expand to sudo apt-get install shotwell wine1.2 pidgin. To grab individual command line arguments, use $1, $2, etc.

Another use for aliases is to let you move around the filesystem easier. Rather than typing cd /var/www/mysite, you could create an alias so you can just type mysite to go right the folder. Here are a few examples:

1
2
3
4
## Directory Shortcuts
alias home='cd ~/'
alias mysite='cd /var/www/mysite'
alias desktop='cd ~/Desktop'

Aliases can even be used to overload built in commands. For example, the rm command is great for deleting files but can be dangerous. To make it safer, create an alias which makes rm always use the -i flag, which prompts before each file it is going to delete:

1
alias rm='rm -i $@'

 

Functions

Functions expand on the functionality of aliases by running multiple commands all together. For example, how about using Google's define feature to look up words on the command line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Define a word - USAGE: define dog
define ()
{
lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 5 -w "*"  | sed 's/;/ -/g' | cut -d- -f5 > /tmp/templookup.txt
            if [[ -s  /tmp/templookup.txt ]] ;then    
                until ! read response
                    do
                    echo "${response}"
                    done < /tmp/templookup.txt
                else
                    echo "Sorry $USER, I can't find the term \"${1} \""                
            fi    
rm -f /tmp/templookup.txt
}

Now try looking up a word on the command line:

1
2
3
4
5
6
$ define apple
* fruit with red or yellow or green skin and sweet to tart crisp
* native Eurasian tree widely cultivated in many varieties for its
* The apple is the pomaceous fruit of the apple tree, species Malus
* The Ariane Passenger PayLoad Experiment (APPLE), was an
* The Apple III (often rendered as Apple ///) is a personal computer

Like aliases, functions can also override commands. For example, you could change the behavior of mv to always change to the directory the files are being moved to after moving them. Alternatively, you can define a similar function (lets call it mvgo) to do this:

1
2
3
4
5
6
7
8
#move and go to dir
mvgo (){
  if [ -d "$2" ];then
    mv $1 $2 && cd $2
  else
    mv $1 $2
  fi
}

 

Conclusion

These examples only scratch the surface of what is possible with .bashrc. Take a look around Google for users' .bashrc files as well as looking at CommandlineFu and this UbuntuForums thread.

Partner Links:
Last Updated on Tuesday, 20 July 2010 22:34  
Related Articles

» A Brief Introduction to Grep

One of the most useful features of operating from a linux terminal is being able to easily perform functions like grep, which searches a group of files for a matching string.  It can return lines with the matching expression, lines without the matching expression, the number of lines matched, and more.  This will serve as a basic tutorial and should cover most usage models; grep is an extremely powerful tool.  Consult the grep manual for a much more detailed description....

» Redirect all output in shell script to a file

Redirecting a single command in a script to a file is straight-forward using the > syntax. For example, to redirect all STDERR output:1echo "hello, world" 2>> /tmp/filename.logTo redirect all lines of a Bash script to a file, add a scope around all of the lines and then redirect the scope to a file. For example:123456(echo "this is line 1 of my script"cat /proc/cpuinfodmesgecho "this is the end of my script") 2>> /tmp/filename.logThis allows you to capture all output of a script...

» How to Determine Which Programs are Using Which Ports

Sometimes a program is using a particular port, preventing another program from capturing and using it. It can be difficult to trace down which process exactly is using the port. Fortunately, there are a couple helpful utilities which can link each process with the port(s) it is using.WindowsOn Windows, download and run . It will display a table of the currently running processes and the ports they are using. It is possible to sort by process name or the port number to make it easy to find the...

Comments  

 
0 #1 Garret Staus 2010-07-21 07:51
This is some great info to know, thanks. Hard to believe I've been using bash this long without knowing this stuff.