Feature
Bash
TC Shell
User prompt
$
%
Assignment statement format
var
=
value
set var
=
value
Export statement
export var
=
value
setenv var value
Alias definition format
alias term
=
command
alias term command
Default user definition file
.bashrc
.cshrc
Automated spelling correction
no
yes
62
◾
Linux with Operating System Concepts
make them available to users. The reason for this is that the entire translation process can be
time consuming (seconds, minutes, even hours for enormous programs). The typical user
will not want to wait while the program is converted, instruction by instruction, before it
can be executed. So we compile the program and release the executable code. In addition,
many users may not have the capability of running the compiler/interpreter or understand-
ing errors generated during the translation process.
There are many situations where interpreting comes in handy. These primarily occur
when the amount of code to be executed is small enough that the time to interpret the code
is not noticeable. This happens, for instance, with scripts that are executed on a web server
(server-side scripting) to produce a web page dynamically or in a web browser to provide
some type of user interaction. As the scripts only contain a few to a few dozen instructions,
the time to interpret and execute the code is not a concern.
There are numerous interpreted programming languages available although histori-
cally, most programming languages have been compiled. One of the earliest interpreted
languages was LISP, a language developed for artificial intelligence research. It was decided
early on that a LISP compiler could be developed allowing large programs to be compiled
and thus executed efficiently after compilation. This makes LISP both an interpreted lan-
guage (the default) and compiled (when the programmer uses the compiler) giving the
programmer the ability to both experiment with the language while developing a program
and compile the program to produce efficient code. More recent languages like Ruby and
Python are interpreted while also having their own compilers.
In the 1990s, a new scheme was developed whereby a program could be partially com-
piled into a format called
byte code
. The byte code would then be interpreted by an inter-
preter. The advantage of this approach is that the byte code is an intermediate version
which is platform independent. Thus, a program compiled into byte code could potentially
run on any platform that has the appropriate interpreter. In addition, the byte code is close
enough to executable code that the interpreting step is not time consuming so that run-
time efficiency is at least partially retained from compilation. Java was the first program-
ming language to use this approach where the interpreter is built into software known as
the Java Virtual Machine (JVM). Nearly all web browsers contain a JVM so that Java code
could run inside your web browser. Notice that JVM contains the term “virtual machine.”
The idea behind the JVM is that it is a VM capable of running the Java environment, unlike
the more generic usage of VM introduced in Chapter 1.
2.6.2 Interpreters in Shells
When it comes to operating system usage, using an interpreter does not accrue the same
penalty of slowness as with programming languages. There are a couple of reasons for this.
First, the user is using the operating system in an interactive way so that the user needs to
see the result of one operation before moving on to the next. The result of one operation
might influence the user’s next instruction. Thus, having a precompiled list of steps would
make little or no sense. Writing instructions to be executed in order will only work if the
user is absolutely sure of the instructions to be executed.
The Bash Shell
◾
63
Additionally, the interpreter is available in a shell and so the operating system is run-
ning text-based commands. The time it takes to interpret and execute such a command
will almost certainly be less than the time it takes the user to interact with the GUI, which
is much more resource intensive.
The other primary advantage to having an interpreter available in the operating system
is that it gives the user the ability to write their own interpreted programs. These are called
scripts. A script, at its most primitive, is merely a list of operating system commands that
the interpreter is to execute one at a time. In the case of Linux, the shell has its own pro-
gramming instructions (e.g., assignment statements, for loops, if–then statements) giving
the user a lot more flexibility in writing scripts. In addition, instructions entered via the
command line can include programming instructions.
The other advantage to interpreted code is that the interpreter runs within an environ-
ment. Within that environment, the user can define entities that continue to persist during
the session that the environment is active. In Linux, these definitions can be functions,
variables, and aliases. Once defined, the user can call upon any of them again. Only if the
item is redefined or the session ends will the definition(s) be lost.
2.6.3 The Bash Interpreter
The Bash interpreter, in executing a command, goes through a series of steps. The first
step is of course for the user to type a command on the command line. Upon pressing the
<
enter
>
key, the interpreter takes over. The interpreter reads the input.
Now, step-by-step, the interpreter performs the following operations:
• The input is broken into individual tokens.
• A token is a known symbol (e.g.,
<<
, |, ~, *, !, etc) or
• A word separated by spaces where the word is a command or
• A word separated by spaces where the word is a filename, directory name, or path or
• An option combining a hyphen (usually) with one or more characters (e.g., -l, -al)
• If the instruction has any quotes, those quotes are handled.
• If an alias is found, that alias is replaced by the right-hand side of the definition.
• The various words and operators are now broken up into individual commands (if
there are multiple instructions separated by semicolons).
• Brace expansion unfolds into individually listed items.
• ~ is replaced by the appropriate home directory.
• Variables are replaced by their values.
• If any of the commands appears either in
` `
or $(), execute the command (we explore
this in Chapter 7).
64
◾
Linux with Operating System Concepts
• Arithmetic operations (if any) are executed.
• Redirections (including pipes) are performed.
• Wildcards are replaced by a list of matching file/directory names.
• The command is executed and, upon completion, the exit status of the command
(if necessary) is displayed to the terminal window (if no output redirection was
called for).
2.7 CHAPTER REVIEW
Concepts and terms introduced in this chapter:
• Alias—A substitute string for a Linux instruction; aliases can be entered at the com-
mand line prompt as in
alias rm
=
‘rm –i’
, or through a file like .bashrc.
• Bash—A popular shell in Linux, it is an acronym for the Bourne Again Shell. Bash
comes with its own interpreter and an active session with variables, aliases, history,
and features of command line editing, tab completion, and so forth.
• Bash session—The shell provides an environment for the user so that the user’s com-
mands are retained, creating a session. The session contains a history of past com-
mands as well as any definitions entered such as aliases, variables, or functions.
• Byte code—An approach taken in Java whereby Java programs are compiled into
an intermediate form so that a JVM can interpret the byte code. This allows com-
piled yet platform-independent code to be made available on the Internet yet does not
require run-time interpreting that a purely interpreted language would require, thus
permitting more efficient execution.
• Command line editing—Keystrokes that allow the user to edit/modify a command
on the command line prompt in a Bash session. The keystrokes mirror those found
in the Emacs text editor.
• Compiler—A program that converts a high-level language program (source code)
into an executable program (machine language). Many but not all high-level lan-
guages use a compiler (e.g., C, C
++
, Java, FORTRAN, COBOL, Ada, PL/I). Prior to
the 1990s, few high-level languages were interpreted (Lisp being among the first) but
today numerous languages are interpreted (e.g., PHP, Perl, JavaScript, Ruby, Python)
but still may permit compilation for more efficient execution.
• C-Shell—A popular Unix and Linux shell that predates Bash.
• Environment variable—A variable defined by a program (usually the operating
system or an operating system service) that can be used by other programs. These
include HOME (the user’s home directory), PWD (the current working directory),
OLDPWD (the previous working directory), PS1 (the user’s command line prompt
definition), and USER (the user’s username) to name a few.
The Bash Shell
◾
65
• History list—Bash maintains the list of the most recent instructions entered by the
user during the current session. This list is usually 1000 instructions long. You can
recall any of these instructions by using the ! character. !! recalls the last instruc-
tion, !
Do'stlaringiz bilan baham: |