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

(1 vote, 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

» Find and Replace Text in Multiple Files

Sometimes it is useful to be able to replace a certain piece of text in multiple files, even across multiple folders. Opening each file and using find/replace is tedious. Fortunately, there is a much easier way using the sed and grep utilities. For Windows users, please to get these programs on your system. Linux and Mac OS X users will already have these programs pre-installed.Launch a terminal window, and cd to the root directory where all of these files and folders exist. Once in the...

» Make an ISO file from Folders or CDROM drive in Linux

If you want to create an ISO image of a CD/DVD or a folder and its contents for archive purposes, copies, easy mounting, burning out, and other purposes, Linux comes with two quick and easy to use command line utilities to make this happen dd and mkisofs.First you will need to open a terminal ("CTRL + ALT + T" in Ubuntu) and type in the following commands for the operation you want to accomplish. CD/DVD Drive:Type: sudo umount /dev/cdrom to unmount your CDRom drive from the...

» Using Cookies to Save Data in PHP

Cookies are a useful way to save information, for example a user preference or login session locally on the client's machine. PHP makes creating and accessing cookies very easy. To create a cookie called myCookie with the value hello,world, simply add the following line of PHP code:$expireTime = 60 * 60 * 24 + (); // 1 day("myCookie", "hello,world", $expireTime); This data will then be available the next time the user loads that particular page. To access the cookie at a later date, use 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.