Lesson 8-Specifying Instructions to the Shell
Lesson 8-Specifying Instructions to the Shell
Overview
An overview of shell.
Execution of commands in a shell.
Shell command-line expansion.
Customizing the functioning of the shell.
Employing advanced user features.
An Overview of Shell
A shell interprets and executes the syntax of the command-
lines in a specific way.
The kernel is the core program of UNIX/Linux, which
schedules processes, allocates memory, and handles
input/output and other peripherals.
User cannot directly communicate with the shell.
The shell interacts with the kernel to execute a request.
The shell is the middleman between the user and the
kernel.
A shell translates a user’s requests into kernel calls.
The login shell is started when a user logs in and exits when
the user logs out.
An Overview of Shell
An Overview of Shell
A shell is the interface between the user, utilities, the file
system, and the kernel.
The shell’s primary function is to read the command-line,
examine its component, and interpret it according to its
rules.
The shell performs the given task and returns the prompt
for further requests.
Execution of Commands in a Shell
Interacting with the shell.
Communicating with the shell.
Identifying utilities for output redirection.
Identifying utilities in pipelines.
Starting processes to run utilities.
Redirecting input and output.
Execution of Commands in a Shell
Passing arguments to processes.
Identifying tokens on the command-line.
The exit code status after a utility execution.
Using the model to interpret command-lines.
Changing the behavior of a command execution.
Interacting with the Shell
Entering a command from the keyboard is the basic way of
communicating with the shell.
For each utility requested by the user, the shell starts a new
child process to execute the code of that utility.
The child process inherits the environment variables like pid,
user, etc.
The “ps” utility can be used for obtaining the process
identification numbers.
Communicating with the Shell
The shell proceeds through a series of specific steps after a
user issues commands.
The complete command-line is first interpreted by the shell.
The shell interprets the ENTER key as the completion of a
command.
The shell interprets “\” as an instruction not to interpret the
special meaning of the single character that immediately
follows it.
Communicating with the Shell
The commands entered at the shell prompt usually include
several words or tokens.
The shell interprets some tokens as utilities and others as
filenames.
The command line interprets the “>”, “|”, and “<“ as
special characters that control the input and output of a file.
The shell uses white space to identify the words or tokens
of a command-line.
The “$” sign is recognized by the shell as the start of a new
variable.
Communicating with the Shell
Identifying Utilities for Output Redirection
The shell interprets the first word in the command-line of the
shell as a utility.
In a C shell, when assigning value to variables, it should not
include any spaces around the “=“ sign.
The value of a set variable can be displayed by adding a $ sign
ahead of the variable name.
The shell interprets the token following the pipe as a utility and
the token following the redirection operator as a file.
Identifying Utilities in Pipelines
A pipeline is a set of one or more utilities that handle data
independently.
The “semicolon” can be used to indicate the end of one
pipeline.
A shell can run one pipeline after another on a single
command-line by separating them with semicolons.
The first token after the semicolon begins a new pipeline, and
hence must be a utility.
Identifying Utilities in Pipelines
The logical AND (&&) operator can instruct the pipeline to
run the next utility based on the success or failure of the
preceding pipeline.
The command-line is successful only if both are executed.
A token following the && operator is interpreted as a utility.
The logical OR (||) operator executes only one of the two
utilities in the command-line.
Identifying Utilities in Pipelines
The shell can easily interpret a variable in all tokens since a
$ sign precedes them.
The “–x” option tells the shell to explain how it interprets
the command-line before executing it.
Starting Processes to Run Utilities
The shell is an active process and runs in the foreground.
The resources allocated to a running process are called
process space or process image.
The shell makes an exact copy of the process space,
including environment variables, when running a utility.
Starting Processes to Run Utilities
A new child process space is an exact copy of the shell.
The child process inherits the input, output, error
destination, and variable information from the parent.
Redirecting Input and Output
The shell interprets the > as an instruction to redirect the
output from the screen to a file.
When redirecting output to a file, a new file is created,
depending on the shell and the noclobber variable set.
Existing files can be protected by setting the noclobber
variable.
Redirecting Input and Output
The csh shell can be instructed to overwrite an existing file by
placing an exclamation point (!) before the redirection symbol
(>).
In bash and ksh shells, the pipe following the redirect is an
instruction to overwrite even if noclobber is set off.
The “–i” option, when used with the mv and cp utilities,
protects files from accidental removal.
A utility, uses the keyboard as default input.
Redirecting Input and Output
The input, the output and the error files all are connected to
the default output, the monitor.
An error message is displayed on the screen if a command
is not able to execute.
An error message can be redirected to a file by using the
“2>” and a filename to the command-line.
The bash, ksh, and sh uses “>” or “1>” to redirect output
to a file.
The standard error and output can both be redirected to the
same file using the “>&” and specifying the filename in the
command line.
Redirecting Input and Output
Passing Arguments to Processes
The “ls” utility interprets an argument as a file if it is not
preceded by a minus sign.
Any tokens left over on the command-line when the shell has
completed the interpretation are passed as an argument to the
associated utility.
The shell also interprets command options as arguments.
Two or more option flags can be specified on the command-line
as one argument.
Identifying Tokens on the Command-Line
The “*” wild card character instructs the shell to include all
the filenames.
The C shell variable path and the family variable PATH
contain a list of directories that the respective shells search
to locate the code for each requested utility.
The /bin directory usually contains all the executables.
Identifying Tokens on the Command-Line
The strings utility ignores all machine code and outputs
only the strings of ASCII characters that it finds.
The shell does not search the path to locate the utility if the
absolute path of the utility is specified.
The Exit Code Status After a Utility Execution
The shell interprets the variable “?” as the exit code of the
last process.
Exit codes other than zero are error codes.
Every time a process completes its execution and exits, it
informs its parent about the status of the exit code.
Using the Model to Interpret Command-Lines
When redirecting output to a file and if the noclobber
variable is set to off, no error message is displayed when
the file is being overwritten.
A utility that requires a filename as an argument will start
reading from the input if the filename is not specified.
A dash (-) argument instructs the sort utility to read from
the input.
Changing the Behavior of a Command Execution
When the shell executes a child process in the foreground,
it waits for a child process to complete execution and then
displays the prompt.
A command consisting of utilities, arguments, and
redirection terminated by ENTER is called a job.
A job can be placed in the background by appending it with
the & sign.
Changing the Behavior of a Command Execution
The ps command can be used for listing all current processes.
A current process in the foreground can be suspended by
pressing CTRL-Z.
The csh, tcsh, bash, and ksh shells allow a user to suspend a
job midstream and return to it later.
The fg command allows a job to be brought back to the
foreground.
Shell Command-Line Expansion
Using shell characters to expand filenames.
Creating and using local variables.
Passing environment variables to child processes.
Using Shell Characters to Expand Filenames
Some characters are interpreted by the shell as wildcard
characters, while others can be used for specifying a range of
characters.
The filename expansion of the filename-matching feature
allows the selection of many filenames while entering only
one name with special characters embedded.
The * and ? are interpreted by the shell as special characters.
Using Shell Characters to Expand Filenames
The asterisk (*) character can be used for matching any
number of characters, while the question mark (?) is used
only for matching a single character.
Shell variable names and values are stored in the memory
and are hence available regardless of the directory location.
Using Shell Characters to Expand Filenames
The shell also allows a range of letters or characters to be
specified with the help of square brackets.
The curly brace characters, “{“ and “}”, are also used by
the bash shell and modern ksh shells for matching and
creating multiple filenames from one pattern.
The curly braces match existing filenames if each match is
specified in the braces, but does not expand ranges.
Creating and Using Local Variables
Local and environmental are the two different kinds of
variables identified by the shell.
The “set” or “env” command lists the variables that are set
in the shell’s memory.
In a csh or tcsh shell, the set command is used for declaring
a variable and assigning a value to it.
Creating and Using Local Variables
In ksh, bash, or sh shell, a variable is directly defined and
assigned a value without the set command.
The shell interprets the $ character as an instruction to
locate in the shell’s memory a variable that has the name
of the character string that follows the $.
The variable must be enclosed in single quotes if it includes
any spaces.
Passing Environment Variables to Child Processes
There are two types of variables - the local variable and the
global variable.
The local variables of a shell are not passed to a child process.
In a C shell, the “setenv” command is used for setting an
environmental variable.
The set, env, or “printenv” commands can be used for listing
the environmental variables.
Passing Environment Variables to Child Processes
The “unset” command can be used for removing a local
variable.
An environmental variable can be removed with the help of
the “unsetenv” command.
The “export” command is used for making a local variable
available to a child process.
Passing Environment Variables to Child Processes
An environmental variable modified by the child process is not
reflected in the parent’s environmental variables.
The shell also allows a variable to be created and exported at
the same time.
The variables set in a child process are lost once the child
process exits.
The child shell takes the memory of the variable when it exits.
Customizing the Functioning of the Shell
Using and modifying the search path.
Creating personalized shell prompts.
Using and Modifying the Search Path
The “path” or “PATH” variable is searched when a user
requests for a utility.
The path is a local variable and is usually assigned a value in
the startup script.
The C shell also maintains a PATH environmental variable,
which also holds the path, and passes it to child processes.
In the C shell, two variables are intertwined where change in
one is automatically reflected in another.
Using and Modifying the Search Path
A single dot is used for denoting the current directory and
can be set in the path variable.
A colon (:) at the beginning or the end of a path string is
interpreted by the sh family of shells as an instruction to
search the current directory.
An empty field using two colons (::) can explicitly request
the current directory, anywhere in the path, using a dot.
Creating Personalized Shell Prompts
In a tcsh or csh shell, the prompt variable is used for
modifying the display prompt.
The “man” and “info” pages describe the collection of
variables that can be used in constructing a prompt.
The ksh and the bash shells use the value in the variable
PS1 as its prompt.
Employing Advanced User Features
Completing filenames:
The variable filec in a tcsh shell, when set in the environment,
instructs the shell to search for matching filenames.
When a shell cannot distinguish between two existing files, it
either displays all matching files or simply flashes or produces
beeps.
Filename completion can also be used for files, directories, and
executables.
Employing Advanced User Features
Completing filenames (continued):
The filename-completion variable can be set in the Korn shell
by executing either the “set –o vi” or the “set –o vi-
tabcomplete” command.
The “set –o vi” or “set +o posix” commands can be used for
turning on the file-completion feature if it is not working.
Many C shells include filename completion, but use the ESC
key to trigger completion of filenames.
Employing Advanced User Features
Evaluating shell variables:
The bash and ksh shells also provide built-in variables that are
useful in interacting with the shell.
The SECOND shell variable can be used for determining the
number of seconds since the shell was started.
In a bash shell, the PROMPT_COMMAND variable allows a user
to execute any command just before it displays the prompt.
A dot file is a run-control file for a specific utility or shell.
Employing Advanced User Features
Customizing shell startup files:
The csh shell can be customized with the help of the .cshrc file
in the /etc directory since it is always read at startup.
The bash shell reads the file .bashrc whenever it starts.
Employing Advanced User Features
Customizing shell startup files (continued):
A system setup to start a ksh file reads the .kshrc file at
startup.
The .kshrc file is not read if the ENV environmental variable is
not set.
The ksh shell is programmed to read at startup whatever file is
the value of the ENV variable.
Summary
All shells accomplish the primary task of interpreting the
commands issued by a user.
A shell process executes code that resides in a file in a
system directory.
A child process started by the shell for each utility
execution inherits the input, output, and error destinations,
as well as environmental variables.
Summary
Redirecting input and output from the default destination to
files and other utilities is one of the functions of a shell.
A command-line inside back quotes is interpreted and
executed as a complete command line.
Local variables are not passed to child processes, while
environmental variables are passed to child processes.
The most recent shells include file completion, which allows
us to type part of a file, directory, or utility name.
A user can place instructions in the startup files to tailor
how the shell functions.
Summary