17 – Shell
December 5, 2009 – 10:16 pmPlease note… This information no longer exists at the referenced locations. This is only a copy of what was available in 2003.
Basic Linux Training™
Shell
Henry White
Table of Contents
Shell Concepts
The shell is a command-language interpreter, much like COMMAND.COM in DOS. This is the interface between the user and the system (more properly in Linux, ‘the kernel’).
Whenever you enter a command it is interpreted by the Linux shell. Some of the commands, such as the print working directory (pwd) command, are built into the Linux bash shell. Other commands, such as the copy command (cp) and the remove command (rm), are separate executable programs that exist in one of the binary directories in the file system (/bin, /usr/bin, /usr/local/bin, /sbin, etc.). Each shell has a slightly different set of built-in commands. The default shell is bash (Bourne Again SHell) which is the GNU version of the original Bourne shell (sh common to every Unix machine on the planet – with some added features and extensions to the built-in commands in the original. In fact, many Unix sysadmins have added bash and prefer it to the original.)
What happens when you enter a command in any of the shells is quite similar to what you’re accustomed to with COMMAND.COM. The shell first checks to see if the command you entered is one of its own built-in commands (like cd or pwd). If it is not, the shell then checks to see if it is a shell script (or other executable) or application program. The shell tries to find these application programs by looking in all of the directories that are in your search path. As the last step in a successful command, the shell’s internal commands and all of the application programs are eventually broken down into system calls and passed to the Linux kernel.
Another very important aspect of the shell is that it contains a very powerful interpretive programming language. This language is similar in function to the DOS interpreted language, but is much more powerful. The shell programming language supports most of the programming constructs found in high-level languages, such as looping, functions, variables, and arrays. And right about now it should be dawning on you ‘why’ there are several shells
The shell programming language is easy to learn, and once known it becomes a very powerful programming tool. Any command that can be typed at the command prompt can also be put into an executable shell program, just as with DOS batch files. This means that the shell language can be used to simplify repetitive tasks. (We will spend more time on Shell programming in upcoming lessons.)
Each user on your system has a default shell. The default shell for each user is specified in /etc/passwd. The system password file contains, among other things, each person’s user ID, an encrypted copy of each user’s password, and the name of the program to run immediately after a user logs into the system. The program specified in the password file does not have to be one of the Linux shells, but it almost always is.
There are several shells you can choose from, although bash is the de facto standard in Linux and is recommended to newbies above any others. Each of the shells has its own advantages and disadvantages, but they are mainly recommended only to those who have used them in Unix. (The Linux versions have many additional features not found on the Unix versions.)
The three most popular shells in Unix are:
- The Bourne shell, written by Steven Bourne, is the original Unix shell and is available on every Unix system in existence. The Bourne shell is considered to be very good for Unix shell programming, but it does not handle user interaction as well as some of the other shells available.
- The C shell, written by Bill Joy, is much more responsive to user interaction. It supports features such as command-line completion that are not in the Bourne shell. The C shell’s programming interface is used by many C programmers because the syntax of its programming language is similar to that of the C language, thus its name.
- The Korn shell (ksh), written by Dr. David Korn, which takes the best features of both the C shell and the Bourne shell and combines them into one that is completely compatible with the Bourne shell.
You can install several shells and switch to any of them on the fly by entering the name of the shell you want to use, then return to your default by typing in exit. However, it is generally a good practice to set up a slightly different prompt for them so you will immediately recognize you are NOT in the default shell.
bash
I’m not going to repeat everything that’s in Chapter 10 on bash; all this will be covered in more detail later. I would simply call your attention to the following features (in no particular order):
- globbing
- wildcards
- ? – any single character
- * – any number of characters (including none)
- [ ] – a set or range of characters
- command history
- aliasing commands
- input/output redirection
- command line editing
- shell variables (customizing your session)
- arithmetic expressions and integer arithmetic
- job control
- directory stacking
- key bindings
- control structures
(We’ll be covering the built-in commands when we get into shell programming.)
Input redirection is not used all that often because most commands that require input from a file have the option to specify a filename on the command line. There are times, however, when you will come across a program that will not accept a filename as an input parameter, you can use input redirection to get around that.
Output redirection enables you to redirect the output from a command into a file, as opposed to having the output displayed to the standard output device (your monitor screen) and is more commonly used than input redirection.
If the output of a command is quite large and will not fit on the screen without scrolling, you might want to redirect it to a file so that you can view it later. There also may be cases where you want to keep the output of a command. Output redirection is also useful if you want to use the output from one command as input for another. (An easier way is to pipe the output of one command as input to a second command.)
pipes are a way to string together a series of commands. This means that the output from the first command in the pipeline is used as the input to the second command in the pipeline. The output from the second command in the pipeline is used as input to the third command in the pipeline, and so on. The output from the last command in the pipeline is the output that you will actually see displayed on-screen (or put in a file if output redirection was specified on the command line).
You can tell bash to create a pipe by typing two or more commands separated by the vertical bar or pipe character: |. A related concept is the use of a filter.
Job control refers to the ability to control the execution behavior of a currently running process. More specifically, you can suspend a running process and cause it to resume running at a later time. Since bash keeps track of all of the processes that it started (as a result of user input), you can suspend a running process or restart a suspended one at any time. Pressing CTRL-Z suspends a running process in the foreground (CTRL-C cancels the running process). The bg command restarts a suspended process in the background, whereas the fg command restarts a process in the foreground.
When a command is started in the foreground, it locks the shell from any further user interaction until the command completes execution. This is usually no problem because most commands only take a few (milli)seconds to execute. If the command you are running is going to take a long time, though, you would typically start the command in the background so that you could continue to use bash for other things – an example of true multiprocessing. (You can also start a process in the background by appending the ampersand (&) at the end of the command line – an good example of this would be updatedb & to update your database of files and installed programs, etc., for example, immediately after you add some additional packages; this is not always run after the initial install, and it wouldn’t hurt to run it as a matter of routine anyhow. I do; and even on my 800 MHz it takes several minutes.)
The bash initialization file is named /etc/profile. Each user who uses bash may have personal preferences, so bash will look for a .bash_profile file in his home directory, otherwise .bash_login or .profile if not found. Typically included in this file are aliasing and shell variables.
Assignments
Some of the material presented here and in your textbook may sound familiar – depending on previous Unix/Linux experience, and how much reading you did during the initial installation and configuration of Linux.
There are, however, several new concepts you need to understand fully, and some terms that are not quite the same in Linux as what you’re accustomed to in DOS. Don’t rush through this! I would encourage you to stick with bash and put the bulk of your efforts into mastering it first before you move on to any of the other shells.
Terms and Concepts:
Define and add these to your glossary:
Utilities & Commands
- &
- *
- [ ]
- |
- .
- ..
- <:
- >:
- >:>:
- ?
- PATH
- bg
- fg
- jobs
- tee
- tr
- CTRL-C
- CTRL-D
- CTRL-Z
Files & Directories
- /dev/null
- /dev/zero
Terms & Concepts
- ambiguous file reference
- append
- argument
- background
- character class
- command
- command line
- device file
- foreground
- end of file (EOF)
- filter
- globbing
- interactive shell
- login shell
- noclobber
- metacharacter
- option
- pathname expansion
- pipe
- process
- process ID (PID)
- redirect
- shell variable
- sleep
- special character
- standard error
- standard input
- standard output
- syntax
- token
- wildcards
- standard error
Online:
- http://www.ling.helsinki.fi/~reriksso/unix/shell.html – An Introduction to the Unix Shell
- http://alge.anart.no/linux/scripts/shell-faq.txt – UNIX shell differences and how to change your shell
- http://web.mit.edu/gnu/doc/html/features_1.html#SEC1 – Bash Features – Bourne Shell Style Features
- http://web.mit.edu/gnu/doc/html/features_toc.html – Bash Features – Table of Contents
- http://www.neosoft.com/man/bash.1.html – /usr/contrib/man/cat1/bash(0)
- http://theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_toc.html – Bash Reference Manual – Table of Contents
- ftp://ftp.cwru.edu/pub/bash/FAQ – BASH Frequently Asked Questions
- http://groups.google.com/ – Newsgroups at Google – put comp.unix.shell in the blank
Copyright © 1997-2003 Henry White. All Rights Reserved.
Reproduction or redistribution without prior written consent is strictly prohibited. Address comments and inquiries to info@basiclinux.net
Sorry, comments for this entry are closed at this time.