The "alias" command
Alias: Speed Dial for your Shell
Download the whole article as PDF
Short URL: http://fsmsh.com/2401
- 2007-09-24
- Server side | Easy
-
Write a full post in response to this!
This content was sponsored by:
You almost certainly have speed dial set up on your home, office and mobile phone. It saves time, ensures against a failing memory and allows you to work smarter.
Devotees of the command line don’t have to be left out in the cold. One of the crown jewels of GNU/Linux is that every user, be he ne’er so base, has at his or her fingertips the kind of power of which even Caligula could not dream. Alright, I’m exaggerating—a little.
GNU/Linux comes with many commands and you use them every time you open a console and interact with it through the shell. The Bash shell (often described as the great grandaddy of all shells or, less flatteringly, as “an historical wart on the Bourne shell”) comes as standard with virtually every version of GNU/Linux and there are others too: Fish, Korn and Zsh. Whether you are listing file contents, configuring your wireless card, copying, deleting or moving files or appending arguments to built-in commands you are utilizing those features.
The built-in command I want to look at is alias. (If you want to be technical, alias has been defined as a “parameterless macro” and is not to be confused with IP aliases, a process for adding more than one IP address to a network interface.) It is a perfect example of a command that is simple yet useful and its use is restricted only by your knowledge of the Unix commands and the capacity of your imagination to exploit it. It is probably at this point that you might want to sit down and think about working faster by working smarter. In short it’s time to get out your pencil and paper and start making a list of all of those commands (and composites thereof) that you use most frequently and see if you can’t make them more compact. In the process you will not only work more efficiently; you will have increased your knowledge of the GNU/Linux commands and file system.
You have mail. You also have aliases
Yes you do. You didn’t install them but they’re there. After you grabbed, burned and booted your chosen distro version they are on your hard drive, but what are they and where they are is the question. So what exactly is an alais? Well, it has exactly the same meaning as it does in the everyday world. If the local bobby (or cop for those in the US) was on your trail you might want to change your name in order to disguise your identity. In the shell world there is no such sinister criminal intent. The purpose is not to deceive but to substitute and shorten.
Where are your default aliases? As ever, it depends on what version of GNU/Linux you are using. It could be in one of three places: .bashrc, .bash_aliases or .profile. Mine is in /etc/bashrc, which means, incidentally, that it is available globally. The default install was fairly spartan and includes only the first four aliases. The rest are my own post-install additions (figure 1).
Creating an alias
So, when you type ls you are already using a built-in alias and your files in any given directory will be displayed, but you might find this a little underpowered. What if you want to list all your hidden files and file permissions? You could type ls -alp and that will do the trick, but by configuring it as an alias you will save time and typing if it is frequently used. To do this, give your alias a name. Clearly, the name needs to be contextual and meaningful to you and above all, it should be short. If you decide on a long name you have defeated the whole purpose of creating an alais in the first place! If you type: alias long="ls -alp" your alias is created. (Note that you cannot have an equals sign in your alias name.) Two points however: first, you can use single or double quotes when creating an alias. Single quotes will save having to select the Shift key and second, you have just made an alias but as you will see the task is not finished just yet.
Make your aliases stick
If I open up a console and make an alias like the one above, it will be available for immediate use. But, if I log out or power down, when I restart and type that name the console will return the classic error message: command not found. Why is that? Simple. Aliases created in a shell will not survive across a reboot and an alias created in one shell will not work in another shell. Here is what I get when I type alias without arguments in my default Bash shell (see figure 2):
If I now open a session of the Korn shell in the console (by typing ksh) and then attempt to list all my aliases, I get what is shown in figure 3.
You can see the difference. However, what you are really interested in is persistent aliases across a reboot regardless of the shell you are using. For this you need to add them permanently in one of the three files listed above. You will need to do this a root, so it might be a good idea to back up those files just in case over zealous or careless keying drops you into a whole world of pain and hurt. To make your new aliases reload the easy way just type . .bashrc or . .bash_aliases if that is where they are on your system. Note that this is a dot (“.”) followed by a space and another dot and the file name. The perceptive amongst you will have worked out that hand-knitting aliases one at a time is a rather tedious business and it might be a good idea to make a list of all the shortcuts you want and key them all in at one pass.
Write a full post in response to this!
Similar articles
Do you like this post?
Vote for it!
Copyright information
Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved.
Biography
An aspiring wanabee--geek whose background is a B.A.(hons) and an M.Phil in seventeenth-century English, twenty five years in local government and recently semi-retired to enjoy my ill-gotten gains.
- Login or register to post comments
- 23977 reads
- Printer friendly version (unavailable!)




Best voted contents
-
Special 301: FOSS users. Now we're all Communists and Criminals
Gary Richmond, 2010-03-05 -
Microsoft's Internet Driving Licence: stupid, unworkable and unenforceable
Gary Richmond, 2010-03-10 -
The Bizarre Cathedral - 69
Ryan Cartwright, 2010-03-12 -
Interview: Nina Paley (author of "Sita Sings the Blues" and the two "Minute Meme" animations)
Terry Hancock, 2010-03-15
Buzz authors
Free Software news
- When are YOU get your copy of this AWSOME FREEsoftware? Check it out at http://bit.ly/5NJCME
- phpMyAdmin 3.3.1 is released http://bit.ly/aERDAA #PHP #MySQL #FreeSoftware #OpenSource (via @TheFox21) #dbms
- #ubuntu #linux Unleashing The ###Ubuntu LoCo Directory http://goo.gl/fb/n4yZ #debian #freesoftware #linux
- #ubuntu #linux ##Ubuntu Global Jam: Time To Rock The House http://goo.gl/fb/EELR #debian #freesoftware #linux
- #FreeSoftware Makes Business Sense http://ur1.ca/q2l4 http://ur1.ca/q2l5
Similar entries
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 60 days
-
Linux performance: is Linux becoming just too slow and bloated?
Mitch Meyran, 2010-01-26 -
Web code is already open - why not make it free as well
Ryan Cartwright, 2010-01-20 -
Save "Sita Sings the Blues" from the Flash format: can you convert FLA?
Terry Hancock, 2010-01-29 -
Microsoft's Internet Driving Licence: stupid, unworkable and unenforceable
Gary Richmond, 2010-03-10 -
Special 301: FOSS users. Now we're all Communists and Criminals
Gary Richmond, 2010-03-05
Hot topics - last 21 days
-
Microsoft's Internet Driving Licence: stupid, unworkable and unenforceable
Gary Richmond, 2010-03-10 -
Special 301: FOSS users. Now we're all Communists and Criminals
Gary Richmond, 2010-03-05 -
The Bizarre Cathedral - 69
Ryan Cartwright, 2010-03-12 -
Interview: Nina Paley (author of "Sita Sings the Blues" and the two "Minute Meme" animations)
Terry Hancock, 2010-03-15
Odiogo
Free Software Magazine uses Apollo, project management and CRM for its everyday activities!




No parameters in bash?
Submitted by clievers on Mon, 2007-09-24 14:54.
Vote!So there's no parameter substitution in bash? That sucks. I was hoping to be able to do something like this:
alias locatemusic='slocate -i "$1" | grep "/home/public/audio/"'But yeah, that doesn't work. Any other way of doing this?
------
let's all play nice!
You could write it as a
Submitted by offset (not verified) on Wed, 2007-09-26 02:06.
Vote!You could write it as a function instead
function locatemusic
{
slocate -i "${1}" | grep "/home/public/audio/";
}
I got it working by creating
Submitted by clievers on Wed, 2007-09-26 17:55.
Vote!I got it working by creating a symlink in the /usr/bin directory to a bash file (containing slocate -i "$1" | grep "/home/public/audio/") on my home drive.
In your example, where would this "function" be placed? Inside of the bash.bashrc file or something?
Thanks.
------
let's all play nice!
cleaner than symlinking in /usr/bin
Submitted by dare2dreamer_ (not verified) on Thu, 2007-09-27 05:43.
Vote!rather than symlinking, you could simply add an addition to your PATH variable:
PATH=$PATH:~/bin
export PATH
Just add the above lines to your .bashrc/.profile and then create a "bin" directory in your user's home. Toss your scripts in ~/bin and they'll work without you having to muck about in /usr/bin as root.
Oh, that's perfect. I'll
Submitted by clievers on Fri, 2007-09-28 17:48.
Vote!Oh, that's perfect. I'll have to give it a shot.
Bit karma points for you.
Cheers :D
------
let's all play nice!