MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

* DocIndex - Basics

Using The Shell VI

* Command Aliases And Shell Functions
* Where To Go From Here
* Shell FAQ

Related Resources:

help alias
man bash

Revision / Modified: Mar.02, 2002 / Mar. 05, 2002
Author: Tom Berger

 

* Command Aliases And Shell Functions

Remembering all sorts of commands and their options and typing them each time is tedious work. Fortunately you don't have to. You can define shortcuts for frequently used commands. These shortcuts can either be defined in the relatively simple form of command aliases or in the somewhat more complex syntax of shell functions.

Command Aliases

For example, I use this command to upload my stuff to MUO:

rsync -e ssh -z -t -r -vv --progress /home/tom/web/muo/rsmuo/docs muo:/www/mandrakeuser/docs

Obviously I'd go nuts if I had to type this command line each time. So I have defined what is called an 'alias' in '~/.bashrc':

alias upmuo='rsync -e ssh -z -t -r -vv --progress /home/tom/web/muo/rsmuo/docs muo:/www/mandrakeuser/docs'

Now all I have to do to upload my stuff is to type upmuo.

The syntax for defining an alias is:

alias shortcut='command'

The quotes are necessary when the command contains empty spaces (e.g. between command and option). Notice that you either quote using single quotes or using double quotes. If you've got a hitch that there's a difference between those two, then you are right ;-).

Single quotes strip the special meaning from all characters included, double quotes from all characters except for '$' (parameter substitution) and '`' (command substitution). Which means in order to use variables or command substitution in aliases, you have to use double quotes. If you look at the example above, I could define a variable called MUOHOME in '.bashrc':

export MUOHOME=$HOME/web/muo/rsmuo/docs

To use that variable in the alias above, I would have to use double quotes:

alias upmuo="rsync -e ssh -z -t -r -vv --progress $MUOHOME muo:/www/mandrakeuser/docs"

because otherwise the alias would search for an directory or file called '$MUOHOME'.

You can create aliases 'on the fly' with the 'alias' command on the command line, or list them in '~/.bashrc' (per user), or in '/etc/profile.d/alias.sh' (for every user and 'root'), in pre-8 Mandrake Linux releases, '/etc/bashrc' fulfills that function. To delete an alias, simply type: unalias alias. Just running alias will list all the defined aliases on your system.

If you have a look at '~/.bashrc' and '/etc/profile.d/alias.sh', you'll see that there are already some aliases defined. You can define more than one alias for the same command. Of course, you must make sure that your alias isn't the name of some other program, something like alias rm='ls -l' won't work. You can try this by typing the shortcut you want to use on the command line. If the shell can't find a command with this name, you can use it as an alias.

Some aliases that might be useful (don't forget the quotes!):

  • alias rpmq='rpm -qa | grep'. Now rpmq string will list all installed RPMs which contain string in their name.
  • alias ls='ls -ho --color | more'. ls will now print a colored and paged listing with file sizes in KB.
  • alias use='du --max-depth=1 | sort -n | more'. use gives you a paged list of subdirectory sizes ordered by size.
  • alias dkd='cd /usr/src/linux/Documentation'. Frequently used directories can be aliased as well. Other prospective candidates are for example the subdirectories of '/mnt'.
    Aliased directories may also be on removable media: alias dlm='/mnt/cdrom/Mandrake/RPMS/'.

One mnemonic hint: try to start aliases with a similar function always with the same letter. For instance, begin all your directory aliases with a 'd'.

I am sure you will find lots of possibilities for using this feature.

Shell Functions

Writing shell functions already boarders on the topic of shell scripting, which is beyond the scope of this article (and mine ;-)). In fact, shell functions are shell scripts, but they have the advantage of being preloaded and being executed in the same shell (whereas a shell script opens at least one sub-shell).

With shell functions, you can do a lot of things you can't do with aliases. One example:

function apros() { apropos $1 | egrep -v '(3|\(n\)'; }

This defines a new command, called 'apros'. apros name will execute 'apropos name' (i.e. a search command for man pages) and pipe (|) the output of that command through 'egrep' which filters out all man pages from sections '3' and 'n', which usually are not of interest, but tend to mess up the output of the 'apropos' command.
Functions allow you to use arguments given to the function name at any place of the function command. With aliases, only one argument is allowed and that argument has to be at the end of the command line (like in the 'rpmq' alias above).
'$1' is a so-called 'positional parameter', it's a placeholder for the first argument given to the function. Of course there are more.

function apros() { apropos $1 | egrep -v "\($2"; }

If you now run the 'apros' command like this:

apros name man_section_number

it searches for name, but excludes all man pages from man_section_number:

apros menu 3

returns all man page titles which contain 'menu', except those from section 3 (programming). Notice that you have to quote twice and that you have to use double quotes:

  • You must quote the 'egrep' search pattern in order to protect it from the shell.
  • You must use double quotes to get the second positional parameter interpreted correctly.
  • You must quote the round bracket again in order to tell 'egrep' to take it literally and not as a special control character .

Tricky, ain't it? ;-).

Shell functions are handled just like aliases: put them into your '.bashrc' to have them around permanently.

* section index * top

*

This article is but the beginning. Shell scripting can help you to automate a lot of tasks, to fix errors in scripts by others yourself and to accustom your Mandrake Linux system to your liking in (almost) every aspect. If you plan to learn one of the more complex programming languages out there, shell scripting is a good place to start because the basic concepts are similar.

The BASH Programming - Introduction HOW-TO will explain the topics covered in this article in more depth and introduce you to the world of shell programming. You can then continue with the very recommendable (and free) Advanced Bash-Scripting Guide by Mendel Cooper.

If you prefer books, I can recommend S. Veeraraghavan's 'Teach Yourself Shell Programming', Sams Publishing. In contrast, I found 'Learning the bash Shell' by Newham/Rosenblatt, O'Reilly, rather unhelpful and confusing, but maybe that's just me ;-).

Apart from that: practice, practice, practice. Read shell scripts by others and try to understand what they do, how and why. Don't run your test scripts as 'root'. Have fun.

* section index * top

* Shell FAQ

How do I turn that &*#! beep off?

With this command:

setterm -blength 0

Why do I get bash: command: command not found?

If it isn't a typo, most likely the command you are trying to execute is located in a directory which is not part of your $PATH. Supply the full path to the command. If you are in the same directory as the command, do ./command.

Why do I get bash: command: Permission denied?

In order for a file to be executable, the execute permission must be set for the user who wants to execute the file. Do a:

chmod 755 file

If that doesn't work, read the article on permissions.

The execution bit is set, I have permissions to execute but permission is denied anyway. Why?

Check the '/etc/fstab' entry of the partition where that file is. Make sure it doesn't contain the option 'noexec'. If it contains the option 'user', the option 'exec' must be set, too.

How do I change the file listing colors?

Copy '/etc/DIR_COLORS' to your home directory and rename it to '.dir_colors'. Everything you need you'll find in that file.

I've put a script 'foo' into my '~/bin' directory, but every time I try to run it, a different command also called 'foo' is started. Why?

Have a look at your $PATH and you will see that your personal '~/bin' directory is the last or at least very close to the end of the $PATH. The shell will search the directories listed in $PATH one after the other. As soon as it finds the first matching command, it executes this command. If there is a command on your system with the same name as your script, it is likely that this command will be executed, not your script. So rename your script.

What does bash: command: bad interpreter mean?

This usually happens with third party installation binaries or Java applications. These applications need a shell of their own, so instead of

command

you have to run

shcommand

My terminal freezes everytime I press <CTRL s>!

Don't do that then ;-). <CTRL s> sends a scroll lock command to the terminal. To release this lock, just press <CTRL q>.

* section index * top


 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.