sections in this module City College of San Francisco - CS160B
Unix/Linux Shell Scripting
Module: Advanced Topics
module list

Shell Options and Aliases

Shell Aliases

An alias is a textual substition performed by the shell on a command. The textual substitution is defined by the alias command:

alias ll='ls -l'

This simple alias defines a new command ll. When the user types ll as a command, the substitution ls -l is made. Thus ll foo becomes ls -l foo

An alias can be self-referencing without any ambiguity, as aliases cannot be recursive. Thus, if you always want the -F option of ls to be turned on, you can set an alias 

alias ls='ls -F'

You can list your aliases by simply typing alias. An alias can be removed by using the unalias command followed by the name of the alias.

Aliases are local to the shell. When you start a new shell, aliases must be recreated. Thus, if you create an alias and start a new shell, that new shell does not have the new alias. They are more like local variables than environment variables, and there is no command for aliases that is equivalent to export.

In bash, aliases are not expanded in shell scripts. This is not standardized among the POSIX-type shells. If you want aliases to be expanded in shell scripts, the shell script must use a bash-specific shell option

shopt expand_aliases

at the beginning of the shell script.

Since aliases are local to the shell, definitions for aliases should go in your ~/.bashrc file rather than your ~/.bash_profile file, so that your aliases are initialized for each shell that starts.

Shell Options

In this section we will discuss the setting of standard shell options available to all POSIX-type shells via the set command. Shell options modify how the shell does certain things.Each option has a name, and many have a single letter abbreviation. There are about 25, but we will only discuss a handful of the most common ones. These options are binary, either on or off, and most are off by default. 

Before we cover shell options we must discuss two very annoying things about them:

We will discuss both of these syntactic problems later.

Here are the most commonly-used options:

option nameabbrevi-
defaultmeaning when "on"
allexport-aoffall variables are exported by default
emacsnoneonuse emacs-style command-line editing (the arrow keys)
noclobber-Coffdon't allow output redirection to overwrite an existing file (now we tell you!)
noglob-foffdon't expand wildcards
nounset-uoffproduce an error if an unset variable is expanded
xtrace-xofftrace the execution of shell commands

Turning on and off shell options

Shell options are toggled using the set directive. You can use the abbreviation, like

set -x

to turn on xtrace, or you can use the option name, as 

set -o xtrace

(think of -o as turning "on" the option)

The problem comes when turning off the option. I don't know where this arcane syntax came from, whether someone just didn't know what to do with on and off started with the same letter, but someone had the bright idea that if -o meant "on", the opposite (off) should be +o. eek. So, to turn off the xtrace option you can use 

set +o xtrace

or (maybe you guessed it)

set +x

This is, of course, counter-intuitive. As it turns out, however, it is so rediculous that it is easy to remember.

Negative options

The other confusing thing about options is that many of them have negative meanings. For example, we all know what happens when you use redirection:

cat file1 > file2

Here, file2 is overwritten, regardless of whether it already existed. There is a shell option to avoid overwriting existing files using redirection. It is called noclobber. So, if you don't want redirection to overwrite an existing file you turn on the noclobber option using 

set -o noclobber     or set -C

Of course if you want to turn it off later you use 

set +o noclobber     or set +C

There is a special output redirection operator >| to redirect to a file even if the noclobber option is set. Thus

cat file1 >| file2

will always overwrite an existing file2 (if it is writable). (Yes, that's a pipe symbol following the output redirection.)


The shell normally expands wildcards. Once in a while, this is a problem. For example, it is useful to use the IFS variable to perform a cut operation for us. If we have a line in the passwd file such as

$ grep "^student:" /etc/passwd

We could use a trick to chop the variable up and assign it to the positional parameters:

$ IFS=:
$ set -- $(grep "^student:" /etc/passwd)
$ echo $1
$ echo $2
The problem here is that the *, which should have been placed in $2 was expanded to a list of the files in the current directory:
$ ls
file2  file3  the other file

We could use the shell option to "turn off wildcard expansion" temporarily before we set the positional parameters:

$ set -o noglob
$ set -- $(grep "^student:" /etc/passwd)
$ echo $1
$ echo $2

Preview question: substring operators, which are the subject of our next section, are used to output part of  a variable's value. If var is "abcdefg", what might the command
echo "${var:2:4}" do? (Hint: assign a variable and test it. Then modify the 2 to a 0 and test it again. Can you figure out what the syntax means?)

Prev This page was made entirely with free software on linux:  

Copyright 2010 Greg Boyd - All Rights Reserved.

Document made with Kompozer