This tutorial has been deprecated! Please use the new version
at LinuxCommand.org

Editing the scripts you already have

Before we get to writing new scripts, I want to
point out that you have some scripts of your own
already. These scripts were put into your home
directory when your account was created, and are
used to configure the behavior of your sessions on
the computer. You can edit these scripts to change
things.

In this lesson, we will look at a couple of
these scripts and learn a few important new
concepts about the shell.

Up to now, we really have not discussed exactly
what commands are. Commands can be several
different things. Some commands are built into the
shell itself. That is, the shell automatically
understands a few commands on its own. The commands
cd and pwd are in this group. Commands
implemented in the shell itself are called shell
builtins. To see a list of the commands built
into bash, use the help
command.

The second type of commands is the executable
programs. Most commands are in this group.
Executable programs are all the files in the
directories included in your path.

The last two groups of commands are contained in
your runtime environment. During your session, the
system is holding a number of facts about the world
in its memory. This information is called the
environment. The environment contains such
things as your path, your user name, the name of
the file where your mail is delivered, and much
more. You can see a complete list of what is in
your environment with the set
command.

The two types of commands contained in the
environment are aliases and shell
functions.

Now, before you become too confused about what I
just said, let's make an alias. Make sure you are
in your home directory. Using your favorite
text editor, open the file .bash_profile and add
this line to the end of the file:

alias l='ls -l'

The .bash_profile file is a shell script that is
executed each time you log in. By adding the alias command to the file, we
have created a new command called "l" which will
perform "ls -l". To try out your new command, log
out and log back in. Using this technique, you can
create any number of custom commands for yourself.
Here is another one for you to try:

alias today='date +"%A, %B %-d, %Y"'

This alias creates a new command called "today"
that will display today's date with nice
formatting.

By the way, the alias
command is just another shell builtin. You can
create your aliases directly at the command prompt;
however they will only remain in effect during your
current shell session. For example:

Aliases are good for very simple commands, but
if you want to create something more complex, you
should try shell functions. Shell functions
can be thought of as "scripts within scripts" or
little sub-scripts. Let's try one. Open
.bash_profile with your text editor again and
replace the alias for "today" with the
following:

function today {
echo "Today's date is:"
date +"%A, %B %-d, %Y"
}

Believe it or not, function is a shell builtin too, and as
with alias, you can enter shell functions directly
at the command prompt.

Since there are many types of commands, it can
become confusing to tell what is an alias, a shell
function or an executable file. To determine what a
command is, use the type
command. type will display
what type of command it is. It can be used as
follows:

Though placing your aliases and shell functions
in your .bash_profile will work, it is not
considered good form. There is a separate file
named .bashrc that is intended to be used
for such things. You may notice a piece of code
near the beginning of your .bash_profile
that looks something like this:

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

This script fragment checks to see if there is a
.bashrc file in your home directory. If
one is found, then the script will read its
contents. If this code is in your
.bash_profile, you should edit the
.bashrc file and put your aliases and
shell functions there.