2.1. Initialization

System-wide configuration files


When invoked interactively with the –login option or when invoked as sh, Bash reads the /etc/profile instructions. These usually set the shell variables PATH, USER, MAIL, HOSTNAME and HISTSIZE.

On some systems, the umask value is configured in /etc/profile; on other systems this file holds pointers to other configuration files such as:

/etc/inputrc, the system-wide Readline initialization file where you can configure the command line bell-style.

the /etc/profile.d directory, which contains files configuring system-wide behavior of specific programs.


On systems offering multiple types of shells, it might be better to put Bash-specific configurations in this file, since /etc/profile is also read by other shells, such as the Bourne shell. Errors generated by shells that don’t understand the Bash syntax are prevented by splitting the configuration files for the different types of shells. In such cases, the user’s ~/.bashrc might point to /etc/bashrc in order to include it in the shell initialization process upon login.

You might also find that /etc/profile on your system only holds shell environment and program startup settings, while /etc/bashrc contains system-wide definitions for shell functions and aliases.

The /etc/bashrc file might be referred to in /etc/profile or in individual user shell initialization files.


Individual user configuration files

Note I don’t have these files?!

These files might not be in your home directory by default; create them if needed.

This is the preferred configuration file for configuring user environments individually. In this file, users can add extra configuration options or change default settings: infra~> cat .bash_profile

This file contains specific settings that are normally only executed when you log in to the system.

Today, it is more common to use a non-login shell, for instance when logged in graphically using X terminal windows. Upon opening such a window, the user does not have to provide a user name or password; no authentication is done. Bash searches for ~/.bashrc when this happens, so it is referred to in the files read upon login as well, which means you don’t have to enter the same settings in multiple files.

In this user’s .bashrc a couple of aliases are defined and variables for specific programs are set after the system-wide /etc/bashrc is read.

This file contains specific instructions for the logout procedure. In the example, the terminal window is cleared upon logout. This is useful for remote connections, which will leave a clean window after closing them.
infra ~> cat .bash_logout

Changing shell configuration files
When making changes to any of the above files, users have to either reconnect to the system or source the altered file for the changes to take effect. By interpreting the script this way, changes are applied to the current shell session

Most shell scripts execute in a private environment: variables are not inherited by child processes unless they are exported by the parent shell. Sourcing a file containing shell commands is a way of applying changes to your own environment and setting variables in the current shell.

This example also demonstrates the use of different prompt settings by different users. In this case, red means danger. When you have a green prompt, don’t worry too much.

Note that source resourcefile is the same as . resourcefile.


o Check the list of files in the user directory.
          ls -la
o Check content in each of the hidden initialization files.
          cat .bash_profile
          cat .bashrc
          cat .bash_logout
o Open /etc/profile and understand the working model.
o Understand working model of .bashrc
o Edit .bash_profile and test.
           echo $TEST
           vi .bash_profile TEST=”Its user defined variable”

           then log off and login
           echo $TEST


2.2. Variables and characters

Types of variables
As seen in the examples above, shell variables are in uppercase characters by convention. Bash keeps a list of two types of variables:

Global variables
Global variables or environment variables are available in all shells. The env or printenv commands can be used to display environment variables. These programs come with the sh-utils package.

Local variables
Local variables are only available in the current shell. Using the set built-in command without any options will display a list of all variables (including environment variables) and functions. The output will be sorted according to the current locale and displayed in a reusable format.

Variables by content
Apart from dividing variables in local and global variables, we can also divide them in categories according to the sort of content the variable contains. In this respect, variables come in 4 types:

String variables
Integer variables
Constant variables
Array variables

Creating variables
Variables are case sensitive and capitalized by default. Giving local variables a lowercase name is a convention which is sometimes applied. However, you are free to use the names you want or to mix cases. Variables can also contain digits, but a name starting with a digit is not allowed:
prompt> export 1number=1
bash: export: `1number=1′: not a valid identifier

To set a variable in the shell, use

Putting spaces around the equal sign will cause errors. It is a good habit to quote content strings when assigning values to variables: this will reduce the chance that you make errors.


Exporting variables
A variable created like the ones in the example above is only available to the current shell. It is a local variable: child processes of the current shell will not be aware of this variable. In order to pass variables to a subshell, we need to export them using the export built-in command. Variables that are exported are referred to as environment variables. Setting and exporting is usually done in one step:

export VARNAME=”value”

Reserved variables

Bourne shell reserved variables
Bash uses certain shell variables in the same way as the Bourne shell. In some cases, Bash assigns a default value to the variable. The table below gives an overview of these plain shell variables:

Table 3-1. Reserved Bourne shell variables

Variable name    Definition
CDPATH               A colon-separated list of directories used as a search path for the cd built-in command.
HOME                  The current user’s home directory; the default for the cd built-in. The value of this variable is also used                                    by tilde expansion.
IFS                         A list of characters that separate fields; used when the shell splits words as part of expansion.
MAIL                     If this parameter is set to a file name and the MAILPATH variable is not set, Bash informs the
                              user of the arrival of mail in the specified file.
MAILPATH           A colon-separated list of file names which the shell periodically checks for new mail.
OPTARG              The value of the last option argument processed by the getopts built-in.
OPTIND               The index of the last option argument processed by the getopts built-in.
PATH                    A colon-separated list of directories in which the shell looks for commands.
PS1                       The primary prompt string. The default value is “‘\s-\v\$ ‘”.
PS2                       The secondary prompt string. The default value is “‘> ‘”.


Bash reserved variables
These variables are set or used by Bash, but other shells do not normally treat them specially.

Table 3-2. Reserved Bash variables

Variable name             Definition

auto_resume                 This variable controls how the shell interacts with the user and job control.
BASH                               The full pathname used to execute the current instance of Bash.
BASH_ENV                      If this variable is set when Bash is invoked to execute a shell script, its value is expanded
                                         and used as the name of a startup file to read before executing the script.
BASH_VERSION              The version number of the current instance of Bash.
BASH_VERSINFO            A read-only array variable whose members hold version information for this
                                          instance of Bash.
COLUMNS                      Used by the select built-in to determine the terminal width when printing selection lists.                                                               Automatically set upon receipt of a SIGWINCH signal.
COMP_CWORD              An index into ${COMP_WORDS} of the word containing the current cursor position.
COMP_LINE                    The current command line.
COMP_POINT                 The index of the current cursor position relative to the beginning of the current command.
COMP_WORDS               An array variable consisting of the individual words in the current command line.
COMPREPLY                    An array variable from which Bash reads the possible completions generated by a shell function                                                invoked by the programmable completion facility.
DIRSTACK                        An array variable containing the current contents of the directory stack.
EUID                                 The numeric effective user ID of the current user.
FCEDIT                             The editor used as a default by the -e option to the fc built-in command.
FIGNORE                          A colon-separated list of suffixes to ignore when performing file name completion.
FUNCNAME                     The name of any currently-executing shell function.
GLOBIGNORE                  A colon-separated list of patterns defining the set of file names to be ignored by file name                                                           expansion.
GROUPS                           An array variable containing the list of groups of which the current user is a member.
histchars                          Up to three characters which control history expansion, quick substitution, and tokenization.
HISTCMD                         The history number, or index in the history list, of the current command.
HISTCONTROL                Defines whether a command is added to the history file.

HISTFILE                           The name of the file to which the command history is saved. The default value is ~/.bash_history.
HISTFILESIZE                   The maximum number of lines contained in the history file, defaults to 500.
HISTIGNORE                    A colon-separated list of patterns used to decide which command lines should be saved in the history list.

HISTSIZE                           The maximum number of commands to remember on the history list, default is 500.
HOSTFILE                          Contains the name of a file in the same format as /etc/hosts that should be read when the shell                                                needs to complete a hostname.
HOSTNAME                      The name of the current host.
HOSTTYPE                        A string describing the machine Bash is running on.
IGNOREEOF                     Controls the action of the shell on receipt of an EOF character as the sole input.
INPUTRC                           The name of the Readline initialization file, overriding the default /etc/inputrc.
LANG                                 Used to determine the locale category for any category not specifically selected with a variable                                                 starting with LC_.
LC_ALL                               This variable overrides the value of LANG and any other LC_ variable specifying a locale category.
LC_COLLATE                     This variable determines the collation order used when sorting the results of file name                                                                expansion,and determines the behavior of range expressions, equivalence classes, and collating
                                            sequences within file name expansion and pattern matching.
LC_CTYPE                           This variable determines the interpretation of characters and the behavior of character classes                                                   within file name expansion and pattern matching.
LC_MESSAGES                   This variable determines the locale used to translate double-quoted strings preceded by a “$”                                                     sign.
LC_NUMERIC                     This variable determines the locale category used for number formatting.
LINENO                               The line number in the script or shell function currently executing.
LINES                                   Used by the select built-in to determine the column length for printing selection lists.
MACHTYPE                         A string that fully describes the system type on which Bash is executing, in the standard GNU                                                     CPU-COMPANY-SYSTEM format.
MAILCHECK                        How often (in seconds) that the shell should check for mail in the files specified in the MAILPATH                                               or MAIL variables.
OLDPWD                             The previous working directory as set by the cd built-in.

OPTERR                               If set to the value 1, Bash displays error messages generated by the getopts built-in.
OSTYPE                               A string describing the operating system Bash is running on.
PIPESTATUS                       An array variable containing a list of exit status values from the processes in the most recently                                                   executed foreground pipeline (which may contain only a single command).
POSIXLY_CORRECT            If this variable is in the environment when bash starts, the shell enters POSIX mode.
PPID                                     The process ID of the shell’s parent process.
PROMPT_COMMAND        If set, the value is interpreted as a command to execute before the printing of each primary                                                        prompt (PS1).
PS3                                       The value of this variable is used as the prompt for the select command. Defaults to “‘#? ‘”
PS4                                       The value is the prompt printed before the command line is echoed when the -x option is set;                                                    defaults to “‘+ ‘”.
PWD                                     The current working directory as set by the cd built-in command.
RANDOM                             Each time this parameter is referenced, a random integer between 0 and 32767 is generated.                                                    Assigning a value to this variable seeds the random number generator.
REPLY                                   The default variable for the read built-in.
SECONDS                             This variable expands to the number of seconds since the shell was started.
SHELLOPTS                          A colon-separated list of enabled shell options.
SHLVL                                   Incremented by one each time a new instance of Bash is started.
TIMEFORMAT                      The value of this parameter is used as a format string specifying how the timing information for                                                 pipelines prefixed with the time reserved word should be displayed.
TMOUT                                 If set to a value greater than zero, TMOUT is treated as the default timeout for the read built-in.                                                 In an interative shell, the value is interpreted as the number of seconds to wait for input after                                                   issuing the primary prompt when the shell is interactive. Bash terminates after that number of                                                 seconds if input does not arrive.
UID                                        The numeric, real user ID of the current user.

Check the Bash man, info or doc pages for extended information. Some variables are read-only, some are set automatically and some lose their meaning when set to a different value than the default.

Special parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

Table 3-3. Special bash variables

Character    Definition
$*                  Expands to the positional parameters, starting from one. When the expansion occurs within double quotes,                           it expands to a single word with the value of each parameter separated by the first character of the IFS                                   special variable.
$@                 Expands to the positional parameters, starting from one. When the expansion occurs within double quotes,                           each parameter expands to a separate word.
$#                  Expands to the number of positional parameters in decimal.
$?                   Expands to the exit status of the most recently executed foreground pipeline.
$-                    A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or                           those set by the shell itself (such as the -i).
$$                   Expands to the process ID of the shell.
$!                    Expands to the process ID of the most recently executed background (asynchronous) command.
$0                   Expands to the name of the shell or shell script.
$_                    The underscore variable is set at shell startup and contains the absolute file name of the shell or script                                    being executed as passed in the argument list. Subsequently, it expands to the last argument to the                                        previous command, after expansion. It is also set to the full pathname of each command executed and                                  placed in the environment exported to that command. When checking mail, this parameter holds the name                          of the mail file.

Note $* vs. $@
The implementation of “$*” has always been a problem and realistically should have been replaced with the behavior of “$@”. In almost every case where coders use “$*”, they mean “$@”. “$*” Can cause bugs and even security holes in your software.

Script recycling with variables

Apart from making the script more readable, variables will also enable you to faster apply a script in another environment or for another purpose.

Quoting characters

A lot of keys have special meanings in some context or other. Quoting is used to remove the special meaning of characters or words: quotes can disable special treatment for special characters, they can prevent reserved words from being recognized as such and they can disable parameter expansion.


Escape characters

Escape characters are used to remove the special meaning from a single character. A non-quoted backslash, \, is used as an escape character in Bash. It preserves the literal value of the next character that follows, with the exception of newline. If a newline character appears immediately after the backslash, it marks the continuation of a line when it is longer that the width of the terminal; the backslash is removed from the input stream and effectively ignored.

Single quotes

Single quotes (”) are used to preserve the literal value of each character enclosed within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Double quotes

Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the backticks (backward single quotes, “) and the backslash.

The dollar sign and the backticks retain their special meaning within the double quotes.

The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or newline. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don’t have a special meaning are left unmodified for processing by the shell interpreter.

A double quote may be quoted within double quotes by preceding it with a backslash.

ANSI-C quoting

Words in the form “$’STRING'” are treated in a special way. The word expands to a string, with backslash-escaped characters replaced as specified by the ANSI-C standard. Backslash escape sequences can be found in the Bash documentation.


A double-quoted string preceded by a dollar sign will cause the string to be translated according to the current locale. If the current locale is “C” or “POSIX”, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.


o Check default variables:
      echo $HOME
      echo $MAIL
      echo $PPID
o Execute successful command and failure command and check the result using $?
      echo $?
      echo $?
o Write a script to understand functionality of $1 $2 $3 and $#

      # This script reads 3 positional parameters and prints them out.
      echo “$1 is the first positional parameter, \$1.”
      echo “$2 is the second positional parameter, \$2.”
      echo “$3 is the third positional parameter, \$3.”
      echo “The total number of positional parameters is $#.”
      chmod +x
      ./ one two three
o Understand purporse of \, ‘ and “
      echo $TEST
      echo \$TEST
      echo ‘$TEST’
      echo “$TEST”
o Load variables into a file and run source command
       vi variables.log
       TEST1=”Its test variable 1″
       TEST2=”Its test variable 2″
       echo $TEST1
       echo $TEST2
       source variables.log
       echo $TEST1
       echo $TEST2


2.3. Expansions and aliases

After the command has been split into tokens (see Section, these tokens or words are expanded or resolved. There are eight kinds of expansion performed, which we will discuss in the next sections, in the order that they are expanded.

After all expansions, quote removal is performed.

Brace expansion
Brace expansion is a mechanism by which arbitrary strings may be generated. Patterns to be brace-expanded take the form of an optional PREAMBLE, followed by a series of comma-separated strings between a pair of braces, followed by an optional POSTSCRIPT. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

Brace expansions may be nested. The results of each expanded string are not sorted; left to right order is preserved.

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces. To avoid conflicts with parameter expansion, the string “${” is not considered eligible for brace expansion.

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma. Any incorrectly formed brace expansion is left unchanged.


Tilde expansion

If a word begins with an unquoted tilde character (“~”), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the HOME shell variable. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

If the tilde-prefix is “~+”, the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is “~-“, the value of the shell variable OLDPWD, if it is set, is substituted.

If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a “+” or a “-“, the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs built-in invoked with the characters following tilde in the tilde-prefix as an argument. If the tilde-prefix, without the tilde, consists of a number without a leading “+” or “-“, “+” is assumed.

If the login name is invalid, or the tilde expansion fails, the word is left unchanged.

Each variable assignment is checked for unquoted tilde-prefixes immediately following a “:” or “=”. In these cases, tilde expansion is also performed. Consequently, one may use file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value.

Example: infra ~> export PATH=”$PATH:~/testdir”

~/testdir will be expanded to $HOME/testdir, so if $HOME is /var/home/infra, the directory /var/home/infra/testdir will be added to the content of the PATH variable.


Shell parameter and variable expansion

The “$” character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

When braces are used, the matching ending brace is the first “}” not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

The basic form of parameter expansion is “${PARAMETER}”. The value of “PARAMETER” is substituted. The braces are required when “PARAMETER” is a positional parameter with more than one digit, or when “PARAMETER” is followed by a character that is not to be interpreted as part of its name.

If the first character of “PARAMETER” is an exclamation point, Bash uses the value of the variable formed from the rest of “PARAMETER” as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of “PARAMETER” itself. This is known as indirect expansion.

Command substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:

or like this using backticks:

Bash performs the expansion by executing COMMAND and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

When the old-style backquoted form of substitution is used, backslash retains its literal meaning except when followed by “$”, “`”, or “\”. The first backticks not preceded by a backslash terminates the command substitution. When using the “$(COMMAND)” form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backticks with backslashes.

If the substitution appears within double quotes, word splitting and file name expansion are not performed on the results.

Arithmetic expansion

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.

Evaluation of arithmetic expressions is done in fixed-width integers with no check for overflow – although division by zero is trapped and recognized as an error. The operators are roughly the same as in the C programming language. In order of decreasing precedence, the list looks like this:

Table 3-4. Arithmetic operators

Operator                                                                  Meaning
VAR++ and VAR–                                                      variable post-increment and post-decrement
++VAR and –VAR                                                      variable pre-increment and pre-decrement

– and +                                                                        unary minus and plus
! and ~                                                                        logical and bitwise negation
**                                                                                exponentiation
*, / and %                                                                   multiplication, division, remainder
+ and –                                                                        addition, subtraction
<< and >>                                                                   left and right bitwise shifts
<=, >=, < and >                                                          comparison operators
== and !=                                                                    equality and inequality
&                                                                                  bitwise AND
^                                                                                   bitwise exclusive OR
|                                                                                   bitwise OR
&&                                                                               logical AND
||                                                                                logical OR
expr ? expr : expr                                                     conditional evaluation
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^= and |=        assignments
,                                                                                   separator between expressions

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 (zero) are interpreted as octal numbers. A leading “0x” or “0X” denotes hexadecimal.

Otherwise, numbers take the form “[BASE’#’]N”, where “BASE” is a decimal number between 2 and 64 representing the arithmetic base, and N is a number in that base. If “BASE’#'” is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, “@”, and “_”, in that order. If “BASE” is less than or equal to 36, lowercase and uppercase letters may be used inter changably to represent numbers between 10 and 35.

Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.

Wherever possible, Bash users should try to use the syntax with square brackets:

Process substitution

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of
The process LIST is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the “>(LIST)” form is used, writing to the file will provide input for LIST. If the “<(LIST)” form is used, the file passed as an argument should be read to obtain the output of LIST. Note that no space may appear between the < or > signs and the left parenthesis, otherwise the construct would be interpreted as a redirection.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.


Word splitting

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly “‘<space><tab><newline>'”, the default, then any sequence of IFS characters serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters “space” and “Tab” are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IF whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

Explicit null arguments (“””” or “””) are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.

Note Expansion and word splitting
If no expansion occurs, no splitting is performed.


File name expansion

After word splitting, unless the -f option has been set (see Section 2.3.2), Bash scans each word for the characters “*”, “?”, and “[“. If one of these characters appears, then the word is regarded as a PATTERN, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is disabled, the word is left unchanged.

If the nullglob option is set, and no matches are found, the word is removed. If the shell option nocaseglob is enabled, the match is performed without regard to the case of alphabetic characters.

When a pattern is used for file name generation, the character “.” at the start of a file name or immediately following a slash must be matched explicitly, unless the shell option dotglob is set. When matching a file name, the slash character must always be matched explicitly. In other cases, the “.” character is not treated specially.

The GLOBIGNORE shell variable may be used to restrict the set of file names matching a pattern. If GLOBIGNORE is set, each matching file name that also matches one of the patterns in GLOBIGNORE is removed from the list of matches. The file names . and .. are always ignored, even when GLOBIGNORE is set. However, setting GLOBIGNORE has the effect of enabling the dotglob shell option, so all other file names beginning with a “.” will match. To get the old behavior of ignoring file names beginning with a “.”, make “.*” one of the patterns in GLOBIGNORE. The dotglob option is disabled when GLOBIGNORE is unset.

What are aliases?

An alias allows a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias built-in commands. Issue the alias without options to display a list of aliases known to the current shell.
infra: ~> alias
alias ..=’cd ..’
alias …=’cd ../..’
alias ….=’cd ../../..’
alias PAGER=’less -r’
alias Txterm=’export TERM=xterm’
alias XARGS=’xargs -r’
alias cdrecord=’cdrecord -dev 0,0,0 -speed=8′
alias e=’vi’
alias egrep=’grep -E’
alias ewformat=’fdformat -n /dev/fd0u1743; ewfsck’
alias fgrep=’grep -F’
alias ftp=’ncftp -d15′
alias h=’history 10′
alias fformat=’fdformat /dev/fd0H1440′

alias j=’jobs -l’
alias ksane=’setterm -reset’
alias ls=’ls -F –color=auto’
alias m=’less’
alias md=’mkdir’
alias od=’od -Ax -ta -txC’
alias p=’pstree -p’
alias ping=’ping -vc1′
alias sb=’ssh blubber’
alias sl=’ls’
alias ss=’ssh octarine’
alias tar=’gtar’
alias tmp=’cd /tmp’
alias unaliasall=’unalias -a’
alias vi=’eval `resize`;vi’
alias vt100=’export TERM=vt100′
alias which=’type’
alias xt=’xterm -bg black -fg white &’
infra ~>

Aliases are useful for specifying the default version of a command that exists in several versions on your system, or to specify default options to a command. Another use for aliases is for correcting incorrect spelling.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain “=”. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to ls -F, for instance, and Bash will not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

Aliases are not expanded when the shell is not interactive, unless the expand_aliases option is set using the shopt shell built-in.

Creating and removing aliases

Aliases are created using the alias shell built-in. For permanent use, enter the alias in one of your shell initialization files; if you just enter the alias on the command line, it is only recognized within the current shell.
infra ~> alias dh=’df -h’
infra ~> dh
Filesystem         Size      Used      Avail       Use%     Mounted on
/dev/hda7         1.3G      272M    1018M     22%            /
/dev/hda1         121M    9.4M      105M        9%             /boot
/dev/hda2         13G       8.7G       3.7G        70%            /home
/dev/hda3         13G       5.3G       7.1G        43%            /opt
none                  243M      0           243M         0%           /dev/shm
/dev/hda6         3.9G      3.2G       572M       85%           /usr
/dev/hda5         5.2G      4.3G       725M       86%           /var
infra ~> unalias dh
infra ~> dh
bash: dh: command not found
infra ~>

Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use alias in compound commands.

Aliases are not inherited by child processes. Bourne shell (sh) does not recognize aliases.

o Example for brace expansion.
       echo sp{el,il,al}l
o Example of tilde expansion
       echo ~
       cat ~oracle/scripting/
o Example using backticks
       echo $DATE
       echo $DATE

o Example to use arithmetic expansion.
       echo $((365*24))
       echo $[365*24]
o Example of redirecting output.
       echo “Redirecting data into file” > output.log
o Check list of aliases on the terminal.
o Create and remove temporary alias and verify its functionality.
       alias DT=’date’
       unalias DT
o Create permanent alias
       vi .bash_profile
       alias DT=’date’
       source .bash_profile


2.4. Regular expressions

What are regular expressions?

A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions by using various operators to combine smaller expressions.

The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash.

Regular expression metacharacters

A regular expression may be followed by one of several repetition operators (metacharacters):

Table 4-1. Regular expression operators

Operator          Effect
.                           Matches any single character.
?                          The preceding item is optional and will be matched, at most, once.
*                          The preceding item will be matched zero or more times.
+                          The preceding item will be matched one or more times.
{N}                      The preceding item is matched exactly N times.
{N,}                     The preceding item is matched N or more times.
{N,M}                 The preceding item is matched at least N times, but not more than M times.

–                          represents the range if it’s not first or last in a list or the ending point of a range in a list.
^                         Matches the empty string at the beginning of a line; also represents the characters not in the range of a                                 list.
$                         Matches the empty string at the end of a line.
\b                       Matches the empty string at the edge of a word.
\B                       Matches the empty string provided it’s not at the edge of a word.
\<                       Match the empty string at the beginning of word.
\>                       Match the empty string at the end of word.

Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated subexpressions.

Two regular expressions may be joined by the infix operator “|”; the resulting regular expression matches any string matching either subexpression.

Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules.

Basic versus extended regular expressions

In basic regular expressions the metacharacters “?”, “+”, “{“, “|”, “(“, and “)” lose their special meaning; instead use the backslashed versions “\?”, “\+”, “\{“, “\|”, “\(“, and “\)”.

Check in your system documentation whether commands using regular expressions support extended expressions.


2.5. grep examples

What is grep?

grep searches the input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or whatever other sort of output you have requested with options.

Though grep expects to do the matching on text, it has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one.

Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

Grep and regular expressions
Note If you are not on Linux

We use GNU grep in these examples, which supports extended regular expressions. GNU grep is the default on Linux systems. If you are working on proprietary systems, check with the -V option which version you are using. GNU grep can be downloaded from

Character classes

A bracket expression is a list of characters enclosed by “[” and “]”. It matches any single character in that list; if the first character of the list is the caret, “^”, then it matches any character NOT in the list. For example, the regular expression “[0123456789]” matches any single digit.

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale’s collating sequence and character set. For example, in the default C locale, “[a-d]” is equivalent to “[abcd]”. Many locales sort characters in dictionary order, and in these locales “[a-d]” is typically not equivalent to “[abcd]”; it might be equivalent to “[aBbCcDd]”, for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value “C”.

Finally, certain named classes of characters are predefined within bracket expressions. See the grep man or info pages for more information about these predefined expressions.


Use the “.” for a single character match. If you want to get a list of all five-character English dictionary words starting with “c” and ending in “h” (handy for solving crosswords): cathy ~> grep ‘\<c…h\>’ /usr/share/dict/words
If you want to display lines containing the literal dot character, use the -F option to grep.

For matching multiple characters, use the asterisk. This example selects all words starting with “c”
and ending in “h” from the system’s dictionary: cathy ~> grep ‘\<c.*h\>’ /usr/share/dict/words

o Use grep to filter an expression from the file.
          grep root /etc/passwd
o Grep expression from file with line number.
          grep -n root /etc/passwd
o Grep everything from a file other than expression.
          grep -v root /etc/passwd
o Grep expression from group of files in a directory.
          grep -i bash ./*.sh
o Exclude an expression in grep from group of files.
          grep -i bash ./*.sh | grep -v bin
o Grep from a file with lines starting with regular expression.
          grep ^root /etc/passwd
o Grep from a file with lines ending with regular expression.
          grep bash$ /etc/passwd
o Grep regular expression with blank spaces on either side.
          grep -w / /etc/passwd
o Grep individual characters in a regular expression.
          grep [yf] /etc/group

o Grep for regular expression starting and ending with a character and fixed number of characters in between.
          grep ‘\<c…h\>’ /usr/share/dict/words
o Grep for regular expression starting and ending with a character and floating number of characters in between.
          grep ‘\<c.*h\>’ /usr/share/dict/words


2.6. Pattern matching

Character ranges
Apart from grep and regular expressions, there’s a good deal of pattern matching that you can do directly in the shell, without having to use an external program.

As you already know, the asterisk (*) and the question mark (?) match any string or any single character, respectively.

Quote these special characters to match them literally.


Character classes

Character classes can be specified within the square braces, using the syntax [:CLASS:], where CLASS is defined in the POSIX standard and has one of the values

“alnum”, “alpha”, “ascii”, “blank”, “cntrl”, “digit”, “graph”, “lower”, “print”, “punct”, “space”, “upper”, “word” or “xdigit”.


o Filter files with range of characters in the location.
        cd /etc
        ls -ld [a-cx-z]*
o Filer files with digits in the name or upper case character in the name.
        ls -ld [[:digit:]]*
        ls -ld [[:upper:]]*


To continue with Tutorial 3, please click here

To start reading from Chapter 1, please click here