Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Linux Tutorial

· Shells and Utilities
· The Shell
· The Search Path
· Directory Paths
· Shell Variables
· Permissions
· Regular Expressions and Metacharacters
· Quotes
· Pipes and Redirection
· Interpreting the Command
· Different Kinds of Shells
· Command Line Editing
· Functions
· Job Control
· Aliases
· A Few More Constructs
· The C-Shell
· Commonly Used Utilities
· Looking for Files
· Looking Through Files
· Basic Shell Scripting
· Managing Scripts
· Shell Odds and Ends

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 79 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

Linux Tutorial - Shells and Utilities - Aliases
  Job Control ---- A Few More Constructs  


What is an alias? It isn't the ability to call yourself Thaddeus Jones when your real name is Jedediah Curry. Instead, in a Linux-context it is the ability to use a different name for a command. In principle, personal aliases can be anything you want. They are special names that you define to accomplish tasks. They aren't shell scripts, as a shell script is external to your shell. To start up a shell script, type in its name. The system then starts a shell as a child process of your current shell to run the script.

Aliases, too, are started by typing them in. However, they are internal to the shell (provided your shell uses aliases). That is, they are internal to your shell process. Instead of starting a sub-shell, the shell executes the alias internally. This has the obvious advantage of being quicker, as there is no overhead of starting the new shell or searching the hard disk.

Another major advantage is the ability to create new commands. You can do this with shell scripts (which we will get into later), but the overhead of creating a new process does not make it worthwhile for simple tasks. Aliases can be created with multiple commands strung together. For example, I created an alias, t, that shows me the time. Although the date command does that, all I want to see is the time. So, I created an alias, t, like this:

alias t=`date | cut -c12-16`

When I type in t, I get the hours and minutes, just exactly the way I want.

Aliases can be defined in either the .profile, .login or the .cshrc, depending on your shell. However, as I described above, if you want them for all sub-shells, they need to go in .cshrc. If you are running a Bourne Shell, aliasing may be the first good reason to switch to another shell.

Be careful when creating aliases or functions so that you don't redefine existing commands. Either you end up forgetting the alias, or some other program uses the original program and fails because the alias gets called first. I once had a call from a customer with a system in which he could no longer install software. We tried replacing several programs on his system, but to no avail. Fortunately, he had another copy of the same product, but it, too, died with the same error. It didn't seem likely that it was bad media. At this point, I had been with him for almost an hour, so I decided to hand it off to someone else (often, a fresh perspective is all that is needed).

About an hour later, one of the other engineers came into my cubicle with the same problem. He couldn't come up with anything either, which relieved me, so he decided that he needed to research the issue. Well, he found the exact same message in the source code and it turned out that this message appeared when a command could not run the sort command. Ah, a corrupt sort binary. Nope! Not that easy. What else was there? As it turned out, the customer had created an alias called sort that he used to sort directories in a particular fashion. Because the Linux command couldn't work with this version of sort, it died.

Why use one over the other? Well, if there is something that can be done with a short shell script, then it can be done with a function. However, there are things that are difficult to do with an alias. One thing is making long, relatively complicated commands. Although you can do this with an alias, it is much simpler and easier to read if you do it with a function. I will go into some more detail about shell functions later in the section on shell scripting. You can also find more details in the bash man-page.

On some systems, you will find that they have already provide a number of aliases for you. To see what alias are currently configured, just run alias with no options and you might get something like this:

alias +='pushd .' alias -='popd' alias ..='cd ..' alias ...='cd ../..' alias beep='echo -en "\007"' alias dir='ls -l' alias l='ls -alF' alias la='ls -la' alias ll='ls -l' alias ls='ls $LS_OPTIONS' alias ls-l='ls -l' alias md='mkdir -p' alias o='less' alias rd='rmdir' alias rehash='hash -r' alias unmount='echo "Error: Try the command: umount" 1>&2; false' alias which='type -p' alias you='yast2 online_update'

As you can see there are many different ways you can use aliases.

 Previous Page
Job Control
  Back to Top
Table of Contents
Next Page 
A Few More Constructs


Test Your Knowledge

User Comments:

You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.09 Seconds