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.





Comments
RSS feed for comments to this post