Introduction to the Shell

You will learn about the following today:

Introduction to UNIX

During the late 1960s, UNIX was developed at AT&T Bell Labs on a DEC PDP-7 computer. Until the early 1980s, UNIX was confined to university departments and laboratories connected with the Department of Defense. UNIX's popularity spread as machines became faster and more capable of handling complex tasks and as AT&T pushed into the commercial market. UNIX is a multitasking system, which means that a user can run more than one process at a time. UNIX is also a multiuser system. That is, it enables more than one user to be working on it at one time. It has the capability to distinguish between multiple users and can identify the data of one user from another. Therefore, many users can be working on the same thing at the same time. UNIX is based on a few basic parts. Once you learn these parts, a lot of the mystery surrounding UNIX simply vanishes. In no time, you will be using UNIX comfortably. In fact, the simplicity of these concepts has enabled several versions of UNIX to be developed and customized.

The three major parts are

Logging In

One of the first things you do when you work with UNIX is to log into UNIX and identify yourself. As stated earlier, UNIX is a multiuser system and can track many types of users. When you first face UNIX, it asks for your user name with one word--login--which looks like this:
login: _
In fact, the exact text for the prompt could be completely different from the text shown here. Just look for the string login:. This is the UNIX way to request input. In some cases, you will see a welcome prompt in addition to the login prompt:
Welcome to Emu mpsi login: 
The login prompt is UNIX's way of asking you for your login name. For instance, my login name is kbh, so I would type kbh at the prompt. Generally, the login name (usually your initials) is assigned to you by your UNIX system administrator. There is nothing magical about a login name being your initials; it could your first name, the first two characters followed by your last name, or something else required by the policies at your UNIX site.

Tip: The login name is also referred to as the user name.

Type in your user name and press the Enter key. The Enter key is also called the Return key on some keyboards. After you type in your user name, you will be asked for a password. If you are not assigned a password by your UNIX system administrator, you may not be presented with this prompt.

login: kbh <Enter>
password: _    (nothing will appear as you type; <Enter>
The text (kbh) after the login prompt is my user name. After typing this in, I pressed the Enter key (<Enter>). Remember that your user name may will be different.

At the next line, UNIX requests a password with the password key. Type in your password at this prompt and press the Enter key. The combination of login name and password serves the purpose of uniquely identifying you to UNIX as an known user. Do not type in the string <ENTER>, just press the Enter key.

Note: My password, for example, is iamhere. Nothing appears as you type in your password. This invisibility prevents others from looking over your shoulder as you type in your password. It also means, however, that you have to type your password without seeing what you type on the screen. Your password is your secret key word into UNIX.
If this is the first time you are logging in to UNIX, you will have been assigned a password. Your password is easy to change after you log in. Depending on your site's policies, you may actually be allowed to continue without a password.

Congratulations! You are logged into the UNIX operating system, and UNIX is ready to accept your commands The $ sign (for the Bourne and Korn shells) is your prompt. If you are using another shell, your prompt could be the % character, the > character, or a combination of characters.

The exact text users see when logging in differs from system to system. Some systems give the time of day, the mail status, and so forth.

Press the Enter key a few times, and you should see the prompt displayed as many times as you press Enter. The following shows an example of this:

$
$
$
By pressing the Enter key several times, you are presented with a prompt indicating that UNIX is ready for more commands. $ _. The prompt can be thought of as "What now, master?" for egocentric users, or "Well, now what do you want me to do?" for meeker users. The prompt will always reappear after you finish using a UNIX command.

Caution: Getting on a system with an account and password is perhaps the hardest thing for new users, and it might be the biggest hurdle you will face when you work with UNIX.

Note: UNIX distinguishes lower- and uppercase. For example, in UNIX, the string EXIT is different from exit.

Thus, if your password were krlpt2 and you typed in krlll2, you would not be allowed access. Similarly, typing in an invalid user name would result in the same problem. See the following examples.

Welcome to Emu
mpsi login: hbg
password: 
(as before, nothing is shown while you type your password)
Login Incorrect 
mpsi login: 
If you mistyped either your login name or password, you would not see the $ sign. Instead, you would see the word Sorry or the string Login Incorrect, and would be asked for your login name again.

See the following for another example.

Welcome to Emu 
mpsi login: Login timeout after seconds. 
Some systems have a timeout (a set time limit) for your to enter you user name after being presented with the login prompt. The timeout is generally set to 60 seconds but can be set to another time limit by your UNIX system administrator. If you see the timeout message, press Enter and you should be presented with a login name as before.

On some secure sites, especially using dialup lines, you will be allowed to log in three times before the system will disable logins on that particular terminal for a long time. Usually this is done on sites where security is an issue and the disable timeout is to prevent someone from trying to log in without having the proper authority. So, if you are particularly dyslexic one day and find the system not responding to your fourth attempt at logging in, contact your system administrator for help.

What Happens When You Are Logged In
When you type in your login name and password, UNIX looks at a file that lists authorized users, their respective passwords, and other information about their accounts. This file contains the passwords in an encrypted form to prevent others from finding out other users" passwords.

If UNIX found that your login name and password matched, it lets you log into the system. UNIX encrypts the password you typed in and compares it with the list of encrypted passwords it keeps on disk. The UNIX standard password scheme makes it very difficult to go from an encrypted password back to the original string. So it's faster and easier to compare the encrypted results of passwords.

UNIX then does some housekeeping and initializes your environment for you by starting up a shell for your login session. The shell provides a prompt and waits for your commands. Your environment includes your current directory and other variables about you as long as you're on UNIX. For more information, see Day 2, "UNIX Tools," and Day 3, "Shell Variables,"respectively.

So, What is Your Shell?

The shell is in itself a program. It is loaded in memory when a user logs in and interacts with the user communicating with the kernel. The shell is also known as the com-mand interpreter because it interprets the commands from the user and passes the commands on to the kernel. The shell is also responsible for returning the results of the commands to the terminal, a file, or another device such as a printer. See Figure 1.1.

Difference Between Program and Command
One of the key concepts of UNIX is that of command and program. A command is something that you request the UNIX system to do and is complete in itself. For example, echo is the command for UNIX to echo the arguments. A sequence of commands is known as a program. A program can be a compiled binary code or a shell script. Compiled code is the output generated from a program (such as C, FORTRAN, Pascal, or another language) and is in a binary format. Binary format is intended to be read by machines only and is thus not readable by a text editor such as vi. Text files are those files that can be read by people and thus can be edited by a text editor such as vi.

Compiled code is generated in several stages, which includes creating the source file, compiling it with a compiler for language, and linking it with libraries of other code modules. The end product of these steps is a compiled binary file.

Shell scripts are text files with a set of UNIX commands that can be edited via a text editor and can be run without going through a compile cycle. Shell scripts are covered extensively in Days 8 through 14. The primary disadvantage of shell scripts (when compared with binary files) is that they run slower than a functionally equivalent, compiled program. Compiled code is better-suited for the kernel because it contains pre-translated read to run code. The shell script has to be interpreted by the shell during execution. UNIX shell scripts provide a flexible, fast way of hooking other scripts and programs together.

When using UNIX to perform a task, you have to decide how to proceed. Do you write a program, a shell script, or both? For example, to do a matrix inversion on a data file, you would be better off writing a compiled program. To do directory operations with types of files, you would write a shell script. In the latter case, your shell script will call other shell scripts, built-in commands, or programs on disk. For example, writing a shell script to do a sort is going to be much slower than writing a shell script that calls the UNIX compiled program 'sort" on disk to do the task for you.

UNIX utilities (such as the shell, binary programs, and shell scripts) reside on disk and are loaded into memory by the kernel when required. When a program is loaded into memory, it is called a process. Naturally, all of the programs on disk cannot be loaded into memory at one time because memory is an expensive commodity and is generally much smaller than disk space. UNIX loads only the programs and data on an as-needed basis. If UNIX sees that there is not enough memory for a newly loaded program, it will make some room for the new program by taking processes from memory to a special area on disk called a swap space. This action is called swapping. When a program finishes running, it is marked as the first one to go when a new program is loaded.

Note: All UNIX utilities do not necessarily have to reside on disk. They can exist on another disk, or even another machine. However, via a special program called mount, UNIX enables other machines to appear as though they are part of a machine. Using mount on disks is beyond the scope of this book. However, keep in mind that the next program you run may not really be on your disk.

Basic Features of a Shell

A shell provides the following features: Don't worry if some of these terms and concepts are confusing now. This book explains all these items in great detail. By the time you are done with this book, these terms will all be meaningful to you.
Command Execution
This function of the shell is to find a command requested by the user and pass it on to the kernel for execution. The shell then passes the results of the command executed by the kernel back to the source that requested the command to be invoked. The source could be the user at the keyboard, in which case, the results are passed on to the terminal. The source could also be another program, another UNIX process or shell script, in which case the results are sent to the invoking program. See Figure 1.2 for the shell's execution process.

Filename Substitution

Shells recognize the *, ?, or [..] as special characters when reading the arguments to a command line. Shells then perform filename expansion on this list before executing the requested program. This way, a program being executed does not have to worry about the expansions. See Day 4 for more information. However, to get a feel for what this means, let's work with an example. Look at the results of the commands on a directory with the files sams, sims, sane, sate, sage, and site.
$ echo s* 
sage 
sams 
sane 
sate 
sims 
site 
$
$ echo s?ms 
sams 
sims 
$
$ echo sa?e 
sane 
sate 
$
$ echo sa*
sage
sams
sane
sate
$
The first command (echo s*) causes the asterisk (*) to be expanded into all the files in this directory. The second command (echo s?ms) causes the question mark to be replaced with one character of the alphabet in the second place of filenames. The third command (echo sa?e) causes the question mark to be replaced with one character of the alphabet in the third place of filenames. The fourth command (echo sa*) causes the sa and asterisk (*) to be expanded to all files in the directory that begin with the letters sa.

I/O Redirection and Pipeline Hookup

Input/output (I/O ) redirection is a function of the shell that redirects the output from a program to a destination other than your screen. This way, you can save the output from a command into a file and redirect it to a printer, a serial port, or even another program. Similarly, the shell can make a program accept input from other than the keyboard by redirecting its input from another source. When you connect the output of one process into the input of another process, you have a pipeline hookup.

For example, the who command reads a list of the currently logged-in users and sends this list to your screen; the wc command prints out a count of the number of lines, characters, and words in its input. You can use pipeline hookup to count the number of logged-in users. Look at the following commands.

$ who 
kamran tty1 Jun 4 20:56
kamran tty2 Jun 4 21:03
ishee tty3 Jun 4 21:41
$ 
$ who | wc 
3 15 93 
The first command (who) gives the results of the who command. Currently, there are two users in this system, with user kamran logged in twice and user ishee logged in once.

The second command (who | wc) redirects the output of the who command to the input of the wc command with the | character. Day 4 covers this in greater detail. The | character is known as the pipe because it pipes the output of the who command to the input of the wc command.

Environment Control

Your shell is fully customizable. You can specify all sorts of personal options. For example, you can display your current directory as part of your prompt or specify a special directory for all your files. Although most of your environment is customizable, some values can either not be changed nor should be changed. These values include your home directory, user id, and login name. These values are required and managed by UNIX to maintain your user identity on the system.

Interpretive Programming Language

Shells enable you to assign values to variables. This is a basic feature of programming along with looping constructs, input, output, and testing conditions. The shell gives you a lot of power, which when combined with the UNIX kernel, can be an extremely powerful tool to work with. In fact, a lot of common tasks can be done with the shell and UNIX utilities, without writing a single line of compilable code.

Shell variables are commonly used as placeholders for arguments whenever long pathnames are too laborious to type. You can create a shell variable and assign it a value that is not so laborious to remember and type. Days 4 and 5 cover the components of the following example in greater detail.

1. $ cd /usr/lib/X11/xdm
2. $ x=--pwd--
3. $ pwd
/usr/lib/X11/xdm
4. $ ch $HOME
5. $ pwd
/home/kamran
6. $ cd $x
7. $ pwd
/usr/lib/X11/xdm
For this particular example and for other special examples, I'll number the lines for later reference. Line 1 requests a change of directory:
/(cd) to /usr/lib/X11/xdm
Line 2 forces the output from the print working directory command (pwd) into the variable x. Note the backward quotes used to take the result of the pwd command as the input into variable x. Day 4 covers this character.

Line 3 represents the request to execute the pwd command, and the following line shows the result from the pwd command.

Line 4 shows how you can get back to your login directory called $HOME. This is the directory you are assigned when you first log into UNIX. Day 3 discusses using the HOME variable.

Line 5 represents the request to execute the pwd command; the next line shows the result from the pwd command. This confirms that you are no longer in the

/usr/bin/X11/xdm directory. Line 6 causes the variable in x to be expanded to the value of /usr/bin/X11/xdm. Thus, line 7 shows that the location of the directory was changed to /usr/bin/X11/xdm.

Remember that the "real" reason UNIX has cryptic commands is that sometimes programmers get lazy typing the same command over and over. The shorter the command, the less likely you are to mistype it. Of course, the fewer the keystrokes, the better. Reducing long pathnames, options, and command names to two-letter combinations is helpful. Only a programmer can really gloat in the feeling of running a four-hour job with the a program called r.

In addition to variable assignment, you also can have loops and tests for conditions. Day 9 covers these in greater detail.

Types of Shells

Shells are independent of the underlying UNIX kernel. This fact has allowed the development of several shells for UNIX systems. Each type of shell has features that enable you to have a choice. There are several versions of the shell available on UNIX systems. A partial list of shell types follows.
The Bourne Shell
This is the most common shell on UNIX systems and was the first major shell. The Bourne shell was released about 1979 and was called sh. If you learn this shell, you will probably be able to work with just about any UNIX system. This shell is distributed as the standard shell on almost all UNIX systems. This shell is named after its author, Stephen Bourne.
The C Shell
This is also called the programmer's shell and exists as csh. It was developed by Bill Joy at the University of California at Berkeley as part of the BSD release. The C shell got its name because its syntax and usage is very similar to the C programming language. Therefore, it's welcomed by C programming aficionados. Unfortunately, the C shell is not always available on all machines. Secondly, shell scripts written in the C shell are not compatible with the Bourne shell. Such scripts have to be modified for working with the Bourne shell. One of the major advantages of the C shell (over the Bourne shell) is its capability to execute processes in the background.
The Korn Shell
The Korn shell was developed by David Korn of AT&T Bell Labs. It's built on the Bourne shell. The most stable version was released in 1988 by AT&T's UNIX System Laboratories as ksh. The initial releases had some serious bugs in them, which were eventually removed.

The Korn shell also incorporates the features of the C shell (such as process control). The Korn shell was initially not available on all UNIX machines but is fast becoming the de facto standard for all later UNIX releases. One of the selling factors for the Korn shell is that it can run Bourne shell scripts without any modification at all.

During its first few releases, the Korn shell had to be purchased as an add-on to UNIX. Given its popularity, several shareware versions of the Korn shell were released after its birth. One is called pdksh and is a public domain Korn shell--thus, it is free.

The public domain version was written by Eric Gisin who made more enhancements to the original Korn shell. These include the emacs customizable keys and the tilde (~) command to default to items other than user names. All in all, this is a good alternative to the original Korn shell (if the original is not available to you on your system). However, it is not as complete as the original Korn shell.

Another later variation of the Korn shell is the windowing Korn shell from USL. This version incorporates the X Windows toolkit features and enables a programmer to write shell scripts that take advantage of the X Windows Xt toolkit. This is a major step toward developing shell scripts that take advantage of the X Windows environment and gives an alternative to the traditional character-based domain for shell scripts. For more information, contact Novell's UNIX System Labs at (800) 847-0240.

The Bourne-Again Shell
This is a freeware shell (called bash) from the Free Software Foundation (FSF) where it was developed by Brian Fox and Chet Ramey. The price (free) is a motivational factor for getting this powerful shell. Also, its "copyleft" decree is worth reading and comparing with a standard software copyright document. The downside to using this free shell is that there is no real support for it (such as you would get from a vendor). You probably wouldn't want to use this shell if you wanted a free alternative to the Korn shell. Remember that some UNIX releases either do not offer the Korn shell. If they do, they might charge you extra. For example, the Linux operating system variant comes with the bash shell as its default shell, and the only Korn shell look-alike is the public domain Korn shell, which is not the same as the original Korn shell.

Using the Shell

As stated earlier, UNIX starts a shell for you when you log in. When the shell starts up, it displays a command prompt as a way of requesting input from the user. The command prompt is generally a dollar sign ($), although the prompt can be different, depending on your mode and type of shell.

A shell can operate in two modes: interactive or noninteractive mode.

A shell is interactive when you, the user, interact with the kernel through this shell. A shell is noninteractive when it executes script files without user intervention. A script file is a set of commands stored in a file on disk. You will learn about script files in Days 8 and 9.

All of the shell scripts you have seen up to now are noninteractive scripts because they do not require any user input while running. In other words, they run from start to finish without requiring any input from the user. For a shell to be interactive, it must interact with the user. That is, it must be able to collect data from the user while running. Day 10 shows you how to determine whether your script is running in interactive mode.

# this is an example of a shell script 
# that runs interactively
# 
echo "Source File"
read src
echo "Destination File" 
read dst
cp $src $dst
The first three lines in this script file are comments only. Comments are notes for the script writer and are ignored by the shell. The shell ignores all characters from the first unquoted # character to the end of the line.

The next line echoes a request for a filename. The line after that reads the user input into a variable src. See Day 10 for more information. You can see that the read command is used to read strings of input (user or via a pipe). This string is then assigned to the variable in the read statement. The input to the read statement is terminated with the Enter key. In this example, the variable src is assigned the string typed in by the user.

The shell then echoes a request for a destination file and reads it in via the next read statement for the dst variable. It then executes the cp copy command using the values in the variable src and dst.

Note: Note the $ sign in front of src and dst to get the value stored in the variables src and dst. If the $ were left out, the cp command would copy the file called src to the file called dst.

Each time the user types in a command, the shell searches a known set of directories for a file with the name of the command. If it finds such a command, it requests the kernel to execute the command. The kernel then loads the command into memory and runs it as a process. The shell then goes to sleep until it receives a response back from the process via the kernel. It then sends the response back to the requesting task or via a pipe to the next process in line.

Command-Line Syntax

UNIX shells are command-line driven. You type in a command and press the Enter key, and the shell processes the command for you and sends the results to where you requested them to go. All shell commands have to follow a syntax. The syntax for a command is:
command [argument1 [argument2 [argument3 ...]]] <Enter>
If present, the brackets around an argument (as in [argument]) indicate that an argument is optional. Special arguments are also called options when they affect the output of a command. All arguments to a command are referred to as the command's argument list. All commands are terminated when the user presses the Enter key. That means that a user can use the Backspace key to correct any typing mistakes.

Caution: If you see ^H every time you press the Backspace key, try the command:

$ stty erase ^H 
where ^H is the character produced when the Backspace key is pressed.

DO remember to use the Backspace key to erase characters to the left of the cursor.

DON'T forget to press Enter to execute your command.

DON'T use the arrows keys on your keyboard; they do not always work. You can get the control character (^H) by pressing the control key (usually marked Ctrl) and lowercase h simultaneously (Ctrl+h).

Single spaces or tabs are used to separate arguments to a command. Spaces are not allowed within arguments. If you have to give spaces within an argument name, use double quotes (") or single quotes (') around an argument.

Note: The accent grave (--) character is a special character that forces the execution of a program of the name quoted between the characters and replaces the quoted string (between the grave characters) with the results of that command before proceeding.

Let's take a sneak preview at what quoting is all about in a shell. You will learn about quote characters and their special functions in Day 4.

$ echo I am learning            about                    you 
I am learning about you
The command in this case is the echo command. Also, there are five arguments to this command: I, am, learning, about, and you. The echo command simply echoes the arguments back out. Note how the extra spaces in the argument list are truncated to one space each.

If you were to put quotes around the entire sentence, you would be requesting the shell to preserve the whitespaces between any shell variables. Consider the output from the following command:

$ echo "I am learning you 
I am learning about you 
$
The quotes allowed the white spaces between learning and about to exist as they were typed in. However, the white spaces between the words about and you were truncated since they were not within quotes.

What is Your Shell?

When you log into a UNIX system, UNIX automatically starts up an interactive shell for you. This is your default login shell. Type the following command.
$ echo $SHELL
/bin/sh
$ _
The output of /bin/sh shows that the current shell is indeed the Bourne shell.

Caution: An easy way to determine what shell you are running is to echo the value of the SHELL variable. This may not be the safest way, however, because any process you run has the potential for changing the SHELL variable to a different value (although it's unlikely). The hard way is to use the ps command to see what shell process you are actually running:

$ ps 
pid tty time command
19028 tty21 0:00 ps
(you will may see other processes listed here as well)
18943 tty21 0:00 sh
$
This output gives process id (pid) for the sh program.

The $SHELL environment variable tells you about what kind of shell you are running. You will learn all about environment variables in Day 4, so hang in there. Just remember that an environment variable is something that holds data with information about your login session.

The echo $SHELL command tells you about your login shell's executable program currently being used.

Some of the other outputs from the echo $SHELL command include the following: For the Bourne-again shell:

$ echo $SHELL
/bin/sh
$
For the C shell (note the lowercase $shell):
% echo $shell
/bin/csh
$
For the Korn shell:
$ echo $SHELL
/bin/ksh
$
Another quick way to tell if you are in the C shell is to see if the prompt is a percent (%) sign. Both the Bourne and Korn shells provide the dollar sign ($) as a prompt. The shareware version of the C shell (notably the shareware version, tcsh) sometimes presents the greater than (>) as its prompt.

However, I caution, as I did before, to use the SHELL environment variable when you want to be sure (you will see ways of changing the prompt in Day 4).

Using Another Shell

If you are discontent with your shell or simply want to try another shell, enter the command to invoke that shell at the prompt. These commands will work only if the shell you are trying to invoke exists on your system. If the shell you are trying to create does not exist on the system, you will get a command not found message and not get a new shell. Because the Korn shell was a commercial shell on some older UNIX machines, these commands to invoke the Korn shell may not work. Similarly, if you do not have the BSD extensions, you may not have the C shell. If you are in doubt, use the which command to find out which version of the shell is available. For example:
$ which csh
/bin/csh
$
This command tells you that the C shell exists in the /bin directory. If the C shell did not exist in the path of executable files, the user would not see any output.

Note: The which command is a very useful tool to have in your toolbox. When writing shell scripts (later in this book), you will want to know which program is being executed. However, the which command may not be available on your system if you do not have the BSD extensions to UNIX. In this case, try the type command to find out which command is being executed. See Day 10 for more details on how to use the which command.

When you enter the command

$ /bin/csh
%
it will invoke the C shell on your terminal. The percent prompt is the shell's way of requesting input. When invoking the Korn shell from within the Bourne shell, you will see no real difference. You have now invoked a new interactive shell for yourself.
$ ksh
$
To get back to the original shell, type exit at the prompt and you will be returned to the calling shell. If the shell into which you typed exit had been your login shell, you would have been logged off the system. For more information, see the section later today titled "Logging Off." These shells are like onion skins wrapped around the kernel. Each new shell is a layer on top of the previous shell; each exit command takes this layer off. The last layer taken off logs you off the system. See Figure 1.3 for this type of layering.
$ csh 
command not found: csh
This tells you that the C shell, csh, does not exist on your current list of paths to look for commands. See Day 3 for details about setting this path.

Note: UNIX offers a find command that locates files for you. For example, to find a file called csh, use the find command. An example follows this note.

$ find   / -name csh       -print
/usr/ucb/bin/csh
$
The arguments to the find command in this example are as follows: If you execute several shells, one after another, you would have to type the same number of exit commands (or Ctrl+d's) to back out of each shell. Think of each shell adding a new layer on top of the kernel. Look at the following command sequence.
$ sh
$ sh
$ sh
$ sh
$ exit
$ exit
$ exit
$ exit
$ exit
bye!
mpsi login: _
The first four invocations of the Bourne shell (sh) required four exit commands. The fifth exit command logged you out completely because it terminated your original login shell. The bye! is a message from the system indicating that it has logged you off the system. You might see a different message, or nothing at all, depending on your system.

Caution: Note that Ctrl+d does not always work at logging you off a shell. Typing the exit command is the preferred, standard way of exiting a shell.

Working with Shell Commands

This section is a prelude to tomorrow and will familiarize you with using UNIX commands. You will use one of the most frequently used commands in UNIX, called ls, for a first trip through UNIX. Do not worry if you do not understand the introduced UNIX terms all at once. By the time you read Day 2, you will be very familiar with all the concepts of files and directories.

Issuing the ls by itself gives a directory listing of everything in your current working directory. Type ls at the prompt, followed by the Enter key.

$ ls 
Xconfig 
Xconfig.new 
advs 
chprog 
chuser 
docs 
faq 
quran 
mkcdrom 
sams
screen.dump 
src 
startx 
tifs 
xme 
The first command line was ls by itself. The results of that were a listing of all the files in the present directory. Now try the same ls command with an argument. Try the following example.
$ ls mkcdrom 
mkcdrom 
This time, the command was of the form: $ ls filename. This format gives a directory listing of all the filenames specified in the command-argument list. In this case, the filename is an optional argument to the ls command.

Thus, because the file mkcdrom existed in my current directory, it was listed to the screen. You can request more than one file in the argument list as well.

$ ls /etc/passwd /etc/group
/etc/passwd /etc/group
This command requests the listings for two files. The command name is ls. The first argument is /etc/passwd, and the second argument is /etc/group. This command will only list the two files in the /etc directory.

Now try the same command (as before) with the -al option.

$ ls -al /etc/passwd /etc/group/etc
-rw-r--r"   1 root     root          258 Jan 28 20:10 /etc/group 
-rw-r--r"   1 root     root          601 Apr 23 22:42 /etc/passwd 
This is command requesting the listings for two files. The command name is ls, and the first argument is -al, which is the request to show all the file (a) and provide a long listing (l).

The second and third arguments are /etc/passwd and /etc/group, respectively. Since these are both files, this command will provide a long listing about these two files in the /etc directory.

Now try the same command, but this time use the ls -al /etc command.

$ ls 
total
drwxr-xr-x	root     root         May 
drwxr-xr-x	root     root         Apr 
		root     root         Feb    DIR_COLORS 
		root     bin          May    crond.pid 
		root     root         Feb    csh.cshrc 
		root     root         Feb    csh.login 
drwxr-xr-x	root     root         Mar    default 
		root     root         Jan    disktab 
		root     root         Jan    fdprm 
drwxr-xr-x	root     root         Apr    fs 
		root     root         Apr    fstab 
		root     root         Jan    gettydefs 
		root     root         Jan    group
lrwxrwxrwx	root     root         Apr    inet
		root     root         Feb    inittab 
		root     root         May    issue 
		root     root         May    klog.pid 
		root     root         Dec    ksh.kshrc 
		root     root         May    ld.so.cache 
		root     root         Mar    ld.so.conf 
drwxr-xr-x	root     root         Apr    lilo 
		root     root         Apr    lilo.conf 
		root     root         Apr    lilo.conf.bak 
		root     root         Dec    magic 
		root     root         Sep    minicom.users 
		root     root         Apr    minirc.dfl 
		root     root         May    motd 
		root     root         May    mtab 
		root     root         Mar    mtools 
		root     root         Apr    passwd 
		root     root         Apr    passwd.OLD 
		root     root         Apr    profile 
drwxr-xr-x	root     root         Apr    rc.d 
		root     root         Jan    securetty 
		root     root         Mar    shells 
drwxr-xr-x	root     root         Apr    skel 
		root     root         Jan    syslog.conf 
		root     root         May    syslog.pid 
		root     root         Mar    termcap 
		root     root         Nov    ttys 
		root     root         May    utmp
This is a very long listing of a directory /etc. The -al is the command's first argument and contains the options -a for all and -l for long. The /etc string is the second argument. In this case, /etc happens to be a directory; so all the files in the /etc directory are shown.

Day 2 examines the output from the ls command in greater detail.

More than One Command Per Line

By now you have dealt with many arguments to one command. Usually, commands are issued one a time at the prompt. However, if you want to issue more than one command on a line, you can separate the commands with semicolons (;).
$ ls who pwd
ls: who: No such file or directory 
ls: pwd: No such file or directory 
This attempts to give you a listing of all who and pwd files.

Now, type in the command again with semicolons between the three words to get:

$ ls;who;pwd
The following code lines show this command with its output.
$ ls;who;pwd 
ch01 
ch02 
ch03 
ch04 
ch05 
ch06 
ch07 
ch08 
ch09 
ch11 
dos2unix 
dos2unix.c 
newkorn.asc 
note 
numlines 
parlor 
phone 
pizza 
sage 
sams 
sane 
sate 
saveme 
sims 
site
toc
whoout
kamran tty2 Jun 5 12:04
kamran tty1 Jun 5 12:00
/home/kamran/sams
The ls command is executed first, the who command next, and then the pwd command.

The semicolons are what differentiates the previous two commands. The semicolons force the execution of the commands in order from left to right, one per command. Spaces are not necessary between the commands and the semicolons.

Because the semicolon is a special character to the shell, you have to put quotes around it to make it an argument instead of a command terminator. Consider the command:

$ ls grunch; bronchial
The command sequence will be to list the file grunch and then execute the command bronchial. However, the command
$ ls "grunch;" bronchial
will be interpreted by the shell as list the file grunch; (that is, with the semicolon as part of the name) and the file bronchial. Notice how the quotes are removed by the shell by the time the argument is passed to the ls command. To see another feature in the shell, try the following command.
$ echo this is a "# " hash 
this is
"#" hash
$ echo # Hi there I am a shell user 
$ (nothing) 
$
The # character is the pound sign directly above the 3 on a keyboard. This is often referred to as the hash character, or the pound sign. As you can see from the output, the shell stopped parsing everything from the # character to the Enter keypress. The next line shows that nothing in the command after the # is interpreted.

This example shows that # is also a command interpreter terminator and takes precedence over the ; operator.

$ pwd #who
/usr/kamran/samples
(the who command is not executed)
The shell will not execute the who command because the # has requested that all further input on the line be ignored until the next command is entered. This is of great significance to those writing shell scripts because the # is used to insert comments into a shell script. So, what if you want to display the # as part of the command structure? Put it in double or single quotes.
$ echo this is "#" hash 
this is # hash
$ echo this is "#" hash 
this is # hash 
As before, the # can be displayed by putting quotes around it. Since the quotes are removed by the shell before the argument is sent to the command, the # is not parsed out and sent to the command.

This is a prelude to Day 4, which covers other complicated aspects of quoting.

Changing Your Password

Let's do some work with another command to change your password. This will help you familiarize yourself with using interactive commands. So far you know that once you issue the command (such as ls), the shell and the ls program do the work without requesting further input from you before returning the results. Other tasks may require some user action during execution. Some examples include changing floppy disk drives for copying files, turning on a printer, and requesting actions required from a program (such as waiting for a keypress in a mail-reading program or changing your password).

Let's work with changing your password for this illustration. You might want to change your user-assigned password to one of your own preference. On some UNIX systems, it is mandatory to change your system-administrator-assigned password to one of your own when you first log in.

You can change your password by using the passwd command. See the following example.

$ passwd 
Enter old password: (nothing is shown as you type)
Enter new password: (nothing is shown as you type)
Re-enter new password: (nothing is shown as you type)
$
The passwd program requires you to type your old password in before allowing a new one. This prevents someone changing a password on an unattended terminal. Also, you have to type the new password in twice to ensure that it is entered correctly.

Choosing a Password

Changing a password seems like an easy task when compared with choosing a password. Because your password is the only means of identifying you and your account to UNIX, guard it carefully and do not disclose to anyone else. Choose a password that is not easy to figure out. For example, it might be easy for someone to figure out your spouse's or children's names and birthdays. Combinations of first names, initials, and last names are very common and easy to guess. On the other hand, passwords like 2kde$5 are quite hard to guess but are a pain to remember. Almost universally, all UNIX systems require at least six characters in a password.

Use your judgment when choosing a password. Can it be kept secret? Is it long enough? Is this password written down somewhere, for example, like a driver's license number?

DO mix upper- and lowercase characters (for example, KrYpToNite, SuPerBirD, and cLARkKeNT).

DO use alphanumeric (a through z and 0 through 9) characters.

DO check to see that your password is not in the online dictionary.

DO make sure that the password is not associated with your name.

DO make sure your password is at least six alphanumeric characters.

DON'T use a common name such as jill, jack, roger, or mike. It's easy to guess the name of a child, spouse, pet, or other loved one.

DON'T use your user, first, or last name. These are easy for others to guess.

DON'T use words found in UNIX's online dictionary.

DON'T keep your accounts with no passwords at all.

DON'T use passwords of project names.

Viewing Long Output On Your Screen

So far, the commands used produce output that is less than 25 characters. Often times, the output goes by too quickly. If you want to stop the output temporarily, type the Ctrl+s command to suspend the screen output. This is done by pressing the Ctrl and s key together.

The application generating the output continues to run; only the output to the screen is suspended. To restart the scrolling display, press Ctrl+q . The output scrolling is restarted until either the output is finished or another Ctrl+s is pressed.

Caution: Pressing several Ctrl+q's at one time may disable the scroll-stopping capability of some terminals. So be cautious when using Ctrl+q. Also, Ctrl+q may not work on some terminals. Try using the more or pg command instead.

To stop the output from a command from scrolling any further, use the Ctrl+c command. This will generally stop the application from generating any more output to the screen.

Logging Off

After your session is over, it's best to log off UNIX. This frees up system resources, and you can walk away from your terminal without leaving a running session for someone else to use. Most UNIX installations log you off automatically if no key is pressed for a certain period. This is something that is a bit of a nuisance if you have a long process running, but for the most part this default is nice to have.

In some cases, your system administrator may have disabled this feature to allow an indefinite login period. In such a case, you cannot rely on UNIX cleaning up after you.

There are two ways to log off the UNIX system:

  1. Type exit at the prompt.
  2. Press Ctrl+d at the prompt.
The exit command instructs your shell to quit. This in turn causes your login session to be terminated, in which case you are presented with a login prompt.

The second command (Ctrl+d) at the prompt sends an end-of-file character to the shell. This tells the shell that there is no more input. Consequently, the shell just quits, leaving you with a login prompt. This is not the preferred way of logging off UNIX because it does not clean up after itself as well as the exit command. This method is included here if you find yourself looking at the login prompt after accidently typing in this combination.

As you learned earlier today, Ctrl+d does not always log you off on some UNIX systems. Typing the exit command is the preferred way of logging off UNIX.

$ exit 
bye! 
Welcome to Emu 
mpsi login: 
The exit command causes the shell to terminate. UNIX cleans up after you and then resets the terminal you were on to accept another user. You may see the word bye instead of the work logout, or some other string, to indicate that you have logged off the system.

On some systems, when you log out, you are presented with a message to indicate that you are indeed off the system.

Summary

You have learned a lot about UNIX today. Today's text introduced you to a broad number of concepts of UNIX rather than going into excruciating detail of one topic or another. As you proceed further, this book will cover each of today's topics in greater detail.

The topics covered today were the following:

UNIX Tools Learned Today

Today's text introduced you to these UNIX tools:

What's Next?

Tomorrow, you'll learn about files, directories, and commands. You will use the knowledge you have gained today to build on the next concepts. Tomorrow's text covers the ls command in more detail and discusses file and directory permissions, and other UNIX tools.

Q&A

Q What is a shell?
A The shell is your interface to the UNIX kernel.
Q How many types of shells are there for UNIX? Name three commonly found shells.
A There are many different types of shells in UNIX, each with its own special characteristics. The three most common shells are the Bourne, C, and Korn shells.
Q Why are there so many shells for UNIX?
A UNIX shells are programs that are independent of the UNIX kernel. You have the complete freedom to write your own personal shell if you want to. Given this freedom of choice, programmers with different tastes and preferences have written their own shells.
Q Why do we need login names and passwords?
A To uniquely identify yourself to UNIX. A password is your key into UNIX.
Q Can more than one login name have more than one password?
A No.
Q What is the command that lists all the files in a directory?
A The ls command is used to list all the files in a directory.
Q What is the preferred way of logging off the system?
A Type exit at the prompt.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered. Some Workshop sections of this book also contain exercises to provide you with experience in using what you've learned. Try to understand the quiz and exercise answers before continuing on to the next chapter. Answers are provided in Appendix D, "Answers."
Quiz
  1. What sequence of events happen when you log into UNIX?
  2. What are the reasons for not showing the password?
  3. Why are passwords stored in an encrypted form?
  4. What's the difference between a command and a program?
  5. What's the difference in doing the ls -al command on a directory instead of a file?
  6. I cannot list two files and a chapter using the
    ls command:
    $  ls file; chapter
    
    Why? How can I change this command to remedy this situation?
Exercises
  1. Type the who command and see who else is logged into your UNIX system.
  2. Find out about the local time on your system. Use the date command.
  3. Select a new password for your account. Be creative.
  4. Try to use two different passwords with the first characters the same. For example, use defer123bad and defer123good. What happens when you try to log in with these passwords?
  5. Try to select a password that is less than six characters long.
  6. Try the following commands in your directory:
    $ ls file; chapter
    $ ls "file cat > help
    This is a help file
    ctrl-d
    $cat > filename
    This is a file called filename
    ctrl-d
    
  7. Then try the following commands:
    $ ls help; chapter
    $ ls "help;" chapter
    $ ls -al
    $ ls -a
    $ ls #
    $ who | wc
    $ date
    $ ls; date; who;