None


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 Wednesday, 21 July 2010 04:34  
Related Articles

» How to Move (or 'Cut and Paste') a File in Linux

Moving a file in linux is as easy as using the mv command:mv filename.txt foo/bar/newname.txtThe command above would move a file named filename.txt into the bar directory with a new name (newname.txt). If you only specify the directory, the name of the file will not change.

» Comments in Python

Line CommentsLine comments in Python are very simple.  The # character denotes that everything afterward on that line is a comment.  12#here is a whole-line comment for some codex = 3 #this comments code on the current line Block CommentsUnfortunately, there is no specific syntax for block comments in python.  To comment a block of code, you must simply place a # before every line in the block of code you wish to comment.  Many Python IDE's, however, provide a simple way...

» How to Shutdown or Restart Windows From the Command Line

To shutdown or restart windows from the command line or using a shortcut, you must use the Shutdown.exe program. To use this program, open up a command prompt window. This can be done by navigating to Start -> Run and typing in 'cmd'.Once the command prompt is open, use the following command:shutdown [-i | -l | -s | -r | -a] [-f] [-m computername] [-t xx] [-c "comment"] [-d up:xx:yy]Arguments:  No args Display this message (same as -?)-i Display GUI interface, must be the...

Comments  

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