D   A   T   A   W   O   K

Creation: January 01 1970
Modified: September 11 2018

Bourne SHell

From the Grymoire Shell Tutorial

To determine the default shell $ echo $SHELL

Creating a shell script

In interactive mode the shell will execute each line you type, until an end of file is found. The end of file (EOF) is issued by typing Control-D. If the EOF is found the Bourne Shell will terminate.

Basic actions, in order:

Meta-characters handling

Special characters handling includes variables evaluation (variables start with a '$') and filename expansion. Expansion of filenames occurs when the characters 'a','?', or '[' occur in a word. An asterisk matches any number of characters; a question mark matches a single character; square brackets are used to specify a range or particular combination of characters. Inside square brackets, a hyphen is used to specify a range of characters. Also, if the first character inside the brackets is an exclamation point, the complement of the range is used. The rules are the same as the regular expressions.

De-facto convention: files whose name starts with a dot are not normally listed by common user programs (e.g. ls,`find').

Filename expansions are based on the current directory, unless the filename starts with a slash.

To be sure on how something will expand, use the echo command to check. It generates a more compact output compared to the ls command.

Dollar signs ($) indicate a variable. Back quote (`) indicates a command.

Finding the executable

Once the shell expands the command line, it breaks up the line into words, and takes the first word as the command to be executed. If the command is specified without an explicit directory path, the shell searches the different directories specified by the 'PATH' variable, until it finds the program specified.


There are

Quoted characters do not have a special meaning

Consider the following:

$ rm -i file1 file2

The shell breaks this line up into four words. The first word is the command while the next three words are passed to the command as three arguments. The interpretation of the arguments is demanded to the command. Is the program that will treat the argument starting with a hyphen as special (an option), not the shell. A filename can containe any character excluded the slash and null.

To include a space in a filename you must quote it. There are three quoting types:

Nested quotations

This is the problem of placing quotation marks within quotation marks.

The double quote is weaker and does not quote a backslash

echo '\'    # prints \
echo "\\"   # prints \
echo \\     # prints \

This does not works as may expected

echo '\''   # This is identical to: echo '

The error is that the \ is not treated as a quotation mark but is just an escaped backslash, then follows the ' that close the quoted text and finally there is another quote begin, that is never closed.

Mixing quotation marks

The easiest way to escape a quotation mark is to use the other form of quotation marks.

echo "Don't forget!"
echo 'Warning! Missing keyword: "end"'

Unlike with most programming laguages, quotation marks in the Bourne shell are not used to define a string. There are used to enable and disable interpretation of meta-characters. The following are equivalent:

echo abcd
echo 'abcd'
echo ab'c'd
echo a"b"cd

Placing variables within strings

Change the quoting mid-stram is also very useful when you are inserting a variable in the middle of a string. You could use weak quotes:

echo "My home directory is $HOME, and my account is $USER"

That is equivalent to this other form:

echo 'My home directory is '$HOME', and my account is '$USER


Variable definition has a very symple syntax:

variable = value

The characters used for variable names is limited to letters, numbers and the underscore character. It cannot start with a number. Whitespace (spaces, tabs or newlines) terminate the value. If you want whitespace in a variable, it must be quoted:

question='What is the filename?'

Multiple assignments can be placed on one line

A=1 B=2 C=3 D=4

There must be no whitespace between the equal sign and the value

a=date      # the date is assigned to a
b = date    # the variable b is the empty string, then the date command is executed

Built-in commands

Built-in commands are the one that comes within the shell itself. They don't involve the invocation of an external command that is usually installed with the system as an independent entity.

The shell treats built-in commands like external commands, and eventually expands the meta-characters before executing the built-in commands.

The set command

Prints all the environment variables in ascending sorted order.

The shell command "export" is used to update environment variables. The command

export a b c 

add the variables "a","b" and "c" to the environment variables set and all child processes will eventually inherit the current value of the variable.

Special Environment Variables


Lists directories that contain commands. When you type an arbitrary command, the directories listed are searched in the specified order. The colon is used to separate directory names. An empty string correspons to the current directory. Therefore the searchpath


contains three directories, withe the current directory being searched first.


Defines where the "cd" goes when is executed without any arguments. The HOME variable is set by the login process.


When you execute the "cd" command, and specify a directory, the shell searches for that directory inside the current working directory. You can add additional directories to this list. If the shell can't find the directory in the current directory, it will look in the list of directories inside this variable.


Lists the characters used to terminate a word. Normally, whitespace separate words and this variable contains a space, a tab and a new line.


Specifies the prompt printed before each command. With the Bourne Shell it is normally "$".


Defines the secondary prompt. That is the prompt you see when you execute multi-line connad, such as "for" or "if". The default value is ">".


Where your mailbox is located. It is set by the login process.

Using curly braces

${var?word} : complain if var is undefined ${var-word} : use word if var is undefined, else use var ${var+word} : user word if var is defined ${var=word} : assign var to word if undefined, then use var