Adding Aliases
Setting aliases can save you even more typing than setting environment variables. With aliases, you
can have a string of characters execute an entire command line. You can add and list aliases with
the
alias
command. Here are some examples of using
alias
from a bash shell:
alias p='pwd ; ls -CF'
alias rm='rm -i'
In the first example, the letter
p
is assigned to run the command
pwd
, and then to run
ls -CF
to
print the current working directory and list its contents in column form. The second runs the
rm
command with the
-i
option each time you simply type
rm
. (This is an alias that is often set auto-
matically for the root user, so that instead of just removing files, you are prompted for each indi-
vidual file removal. This prevents you from automatically removing all the files in a directory by
mistakenly typing something such as
rm *
.)
While you are in the shell, you can check which aliases are set by typing the
alias
command. If
you want to remove an alias, type unalias. (Remember that if the
alias
is set in a configuration
file, it will be set again when you open another shell.)
Using Shell Environment Variables
Every active shell stores pieces of information that it needs to use in what are called environment
variables. An environment variable can store things such as locations of configuration files, mail-
boxes, and path directories. They can also store values for your shell prompts, the size of your his-
tory list, and type of operating system.
To see the environment variables currently assigned to your shell, type the
declare
command.
(It will probably fill more than one screen, so type declare | more. The
declare
command also
shows functions as well as environment variables.) You can refer to the value of any of those
variables by preceding it with a dollar sign (
$
) and placing it anywhere on a command line. For
example:
$ echo $USER
chris
This command prints the value of the
USER
variable, which holds your username (chris).
Substitute any other value for
USER
to print its value instead.
Common Shell Environment Variables
When you start a shell (by logging in or opening a Terminal window), a lot of environment vari-
ables are already set. Table 2-7 shows some variables that are either set when you use a bash shell
or that can be set by you to use with different features.
60
Linux First Steps
Part I