You are on page 1of 64

Shells

What is Shell?
• The shell is a program used to interface
btw user and linux kernel.
• The shell is a command language
interpreter , has its own set of built in shell
commands , makes use of all Linux utilities
& application programs available on the
system.
The relationship between the user and the
shell.
Command interpretation by the shell.

The cp and
rm are
separate
executable
programs
that exist in
one of the
directories
in the
filesystem.
How the shell gets started?
• The shell is started after you successfully log into the
system & it continues to be the main method of
interaction between the user and the kernel until you
logout.

• Each user on your system has a default shell, which is


specified in the system password file called /etc/passwd

• System password file contains each person’s user ID, an


encrypted copy of each user password & the name of
the program to run after a user logs into the system
which is mainly one of th linux shells
The Most Common Shells
• The Bourne shell ( called sh)

• The C shell ( called csh)

• The Korn shell ( called Ksh)


Comparison of Shells
Bourne shell C shell Korn shell

written by Steven Bourne. Bill Joy Dave Korn

User interaction Does not handle Much more Very interactive


responsive
Supports No such Several features Has features of
features like command- Bourne & C
available line completion shell
Programming Good Not as good as Good
interface Bourne shell
Drawback The way it
handles user
input ( typing )
Three New shells
• The Bourne Again Shell ( bash)

• The Public Domain Korn Shell(pdksh)

• The tcsh
Comparison of shells
bash pdksh Tcsh
Extesion of The Bourne The Korn shell The C shell
shell
Fully backward Yes , with Does not Yes with csh
compatible Bourne shell support all
features of Korn
shell
Key binding Not available Available Available
feature
Customization bash pdksh Requires 2 files
initialization file initialization file 1. login file
2. cshrc file
Spell correction Not available Not available Available
feature
Features bash pdksh tcsh
Command cd t<tab> vi s\ lpr s
completion Press enter Press enter Press enter

Wild cards *, ? , […] We use these Same


( cd, lpr) with vi editor
Command .bash_history .ksh_history .history
history history , fc Fc history
Input redirection Same Same Same
O/p redirection Same Same Same

Pipelines Same Same Same


Prompts 2 levels 3 levels 3 levels

Job control Same Same Same

Key bindings Not available Available Available

Spelling Not available Not available Available


correction
Feature bash pdksh tcsh
Variables Same Same Different
Commands Same Same Some are
different
By default It does have Does not have It does have
command line default
completion command
completion
history history No history history
command command exists command exist, command exists
an alias is made
The Bourne Again shell
1. The COMMAND-LINE COMPLETION
2. WILD CARDS (* , ? , […])
3. COMMAND HISTORY
Bash stores previous commands in the history list .
(a) Use of up and down arrow keys
Commands displayed on the command line through the history list
can be edited simply by right and left arrow keys and delete and
backspace keys

(b) Display n edit the hisory list using history and fc ( fix commands)
commands built into bash
b.1 history // displays list of 1000 previous commands
b.2 history [n] // n can be any integer
// only last n lines in the history list is to be
shown
eg history [20]
b.3 history [-r|w|a|n] [filename]

where

-r : read contents of history file & use them as current history list
-w : write the current history list to the history file ( overwrite what is
currently in the file )
-a : appens the current history list to the end of history file
-n : causes the lines in the history file to be read into the current
history list

Note ; if no filename is specified , the history command will use the value of the
HISTFILE shell variable .

b.4 fc [-e editor_name] [-n] [-l] [-r] [first] [last]

all the options given are optional


-e: used to specify the text editor to be used for editing the commands
first , last : used to select a range of commands to take out of the
history list , which canbe a string or a number
-n : used to supress command numbers when listing history commands
-r : lists the matched options in reverse order
-l : lists the matched commands to the screen
Note1: when –l option is not used , the matching command will be loaded into
the text editor .

Note2: The text editor used by fc is found by taking the value of editor name if
the -e editor name option is used. If this option was not used, fc uses the editor
specified by the variable FCEDIT. If this variable does not exist, fc will use the
value of the EDITOR variable. Finally, if none of these variables exists, the editor
that will be chosen is vi, by default.
4. Aliases

Command aliases are commands that the user can specify. Alias
commands are usually abbreviations of other commands, designed to
save keystrokes.

Now, until you exit from bash, the goconfig command will cause the
original, longer command to be executed as if you had just typed it.

unalias command to delete the alias:


Examples of aliases are:
• alias ll='ls -l'
• alias log='logout'
• alias ls='ls -F'
• alias dir='ls'
• alias copy='cp'
• alias rename='mv'
• alias md='mkdir'
• alias rd='rmdir‘

If you enter the alias command without any arguments, it will display all of
the aliases that are already defined on-screen.

alias dir='ls‘
alias ll='ls -l'
alias ls='ls -F'
alias md='mkdir'
alias net='term < /dev/modem > /dev/modem 2> /dev/null&'
alias rd='rmdir'
5. Input redirection

Input redirection is used to change the source of input for a command


Eg 1.
the rm command requires arguments on the command line. You must
tell rm the files that you want it to delete on the command line, or it will
issue a prompt telling you to enter rm -h for help.
Eg 2.
the wc (word count) command counts the number of characters,
words, and lines in the input that was given to it. If you just type wc
<enter> at the command line, wc waits for you to tell it what it should be
counting.

the wc command is collecting input for itself.

If you press Ctrl-D, the results of the wc command will be written to the
screen.
(A) If you enter the wc command with a filename as an argument,it will
return the number of characters, words, and lines that are contained in
that file:
Eg. wc test

11 2 1
(B)To pass the contents of the test file to wc is to redirect the input of
the wc command from the terminal to the test file. This will result in
the same output. The < symbol is used by bash to mean "redirect
the input to the current command from the specified file."

So, redirecting wc input from the terminal to the test file can be done
by entering the following command:
Eg. wc < test

11 2 1
6. Output Redirection

Output redirection is more commonly used than input redirection.


Output redirection enables you to redirect the output from a command
into a file

the > symbol is used.

you can redirect the output of the ls command into a file named
directory.out using the following command:

ls > directory.out
7. Pipelines

(A) Pipelines are a way to string together a series of commands.


output from the first command in the pipeline is used as the input to the
second command in the pipeline.
The output from the last command in the pipeline is the output that you
will actually see displayed on-screen

(B) two or more commands separated by the vertical bar or pipe character, |.

Things to do today:
Low: Go grocery shopping
High: Return movie
High: Clear level 3 in Alien vs. Predator
Medium: Pick up clothes from dry cleaner

cat sample.text | grep "High" | wc -l

2
8. Prompts

bash has two levels of user prompt.

The first level is what you see when bash is waiting for a command to
be typed.

The default first-level prompt is the $ character.

To customize your default first level prompt, set the value of the
PS1 bash variable.

PS1="Please enter a command“

sets the bash shell prompt to the specified string.


The second level of prompt is displayed when bash is expecting more
input from you in
order to complete a command.

The default for the second level prompt is >.

To customize your default second level prompt , set the value of


the PS2 variable, as in

PS2="I need more information"


. Prompt special character codes.

Character Meaning
\! Displays the history number of this command.
\# Displays the command number of the current command.
\$ Displays a $ in the prompt unless the user is root. When the
user is root, it displays a #.
\\ Displays a backslash.
\d Displays the current date.
\h Displays the host name of the computer on which the shell is
running.
\n Prints a newline character. This will cause the prompt to span
more than one line.
\nnn Displays the character that corresponds to the octal value of
the number nnn.
\s The name of the shell that is running.
\t Displays the current time.
\u Displays the username of the current user.
\W Displays the base name of the current working directory.
\w Displays the current working directory.
9. Job control

Job control refers to the ability to control the execution behavior of a


currently running process.

Specifically, you can suspend a running process and cause it to


resume running at a later time.

bash keeps track of all of the processes that it started (as a result of
user input), and you can suspend a running process or restart a
suspended one at any time during the life of that process.

Pressing Ctrl-Z suspends a running process.


The bg command restarts a suspended process in the Background

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

if you started this command in the foreground,


find / -name "test" > find.out
which will scan the entire filesystem for files named test and store the
results in a file called find.out
If u wanted to continue executing in the background so you could use the
system again, you would enter the following:
control-z
bg

This would first suspend the find command, then restart it in the background.
The find command would continue to execute, and you would have bash back
again.
Customising bash
Until now, the changes that you made affected only the current bash
session. As soon as you quit, all of the customizations that you made
will be lost.

You can make the customizations more permanent by storing them in


bash initialization file.

You can put any commands each time bash is started into this initialization file.

Commands that are typically found in this file are alias commands and variable
initializations.

The bash initialization file is named profile. Each user who uses bash has a .profile file in
his home directory. This file is read by bash each time it starts, and all of the commands
contained within it are executed.
.
bash Commands
• alias
• bg
• cd
• exit
• export: Causes the value of a variable to be made visible to all
• subprocesses that belong to the current shell.
• fc
• fg: Foreground command.
• help: Displays help information for bash built-in commands.
• history
• kill.
• pwd
• unalias
bash Variables
EDITOR, FCEDIT: The default editor for the fc bash command.
HISTFILE: The file used to store the command history.
HISTSIZE: The size of the history list.
HOME: The HOME directory of the current user
OLDPWD: The previous working directory (the one that was current
before the current directory was entered).
PATH: The search path that bash uses when looking for executable
files
PS1: The first-level prompt that is displayed on the command line
PS2: The second-level prompt that is displayed when a command is
expecting more input
PWD: The current working directory
SECONDS: The number of seconds that have elapsed since the
current bash session was started
pdksh
1. COMMAND LINE COMPLETION
pdksh does not default to allowing the user to perform command-
line completion. You must enter a command to tell pdksh that you
want to be able to use command-line completion.
In order to enable command-line completion, enter one of the following
commands:
set -o emacs
OR
set -o vi

This causes pdksh to accept command editing that is similar to emacs


or vi.
you can perform command-line completion by pressing the Esc key
twice (when using emacs command-line editing), or by pressing \
(when using vi command-line editing).

If the sample.text file is not the only file in the directory that begins with the
letter s, pdksh completes the command as far as it can and then beeps,
indicating that it needs more information to complete the command
2. Wildcards ( * , ? ,[…])
.
3. COMMAND HISTORY

(a)
Emacs
Scroll up by ctrl-p
Scroll down by ctrl-n

Vi
Scroll up by k or –key
Scroll down by j or +

In vi editor , u enter command mode by pressing esc key


(B) Using history file is to display and edit it using fc (fix command), the
built-in pdksh shell command.

The history command was left out of the pdksh shell because all of
its functionality could be provided by the fc command.

An alias to the fc -l command is history which is used in pdksh

The fc command is used to edit the command history. It has a number


of options,

fc [-e ename] [-nlr] [first] [last]

All options given in braces are optional.


-e : used to specify the text editor that is to be used for editing the
commands in the command history.
first , last : used to select a range of commands to take out of the
history list , which canbe a string or a number
-n :used to suppress command numbers when listing the
history commands that matched the specified range.
-r:lists the matched commands in reverse order.
-l :lists the matched commands to the screen.

Note1: In all cases except for the -l option, the matching commands are
loaded into a text editor.

The text editor used by fc is found by taking the value of ename if the
–e option was used. If this option was not used, fc uses the editor
specified by the variable FCEDIT. If this variable does not exist, fc uses
the value of the EDITOR variable. Finally, if none of these variables
exists, the editor chosen is vi.
If you enter the fc command with no arguments, it loads the last
command that was entered into the editor. fc loads the last
command into the default editor.

fc -l lists the last 16 commands that were entered.

fc -l 5 10 lists the commands with history numbers between 5 and 10,


inclusive.
fc 6 loads history command number 6 into the default editor.

fc mo loads into the default editor the most recent command that starts
with the string mo.
4. Aliases

vi things-to-do-today.txt

alias ttd='vi things-to-do-today.txt‘

unalias ttd
5. Input Redirection

6. Output Redirection

7. Pipelines
8. Shell Prompts

pdksh has three levels of user prompts.

The first level is what the user sees when the shell is waiting for a
command to be typed.

The default prompt is the $ character.


You can customize it by setting the value of the PS1 pdksh variable.
PS1="! Tell me what to do"

The pdksh shell keeps track of how many commands have been entered since
it was started. This number is stored into the shell variable called !. When
you include the ! in the prompt, it displays the current command number in the
prompt. The previous prompt command causes the command number followed
by the string Tell me what to do to be displayed on the command line each
time pdksh is expecting you to enter a command.
The second level of prompt is displayed when pdksh is expecting more input
from you in order to complete a command.

The default for the second level prompt is >.

You can cutomize it by setting the value of the PS2 pdksh variable.
PS2=" I need more information"

pdksh does not support the advanced prompt options that bash
supports.

There is not a predefined set of escape codes that you can put in a pdksh
prompt variable to display such items as the time or current working directory.
You can, however, put other pdksh variables into a prompt variable. For
example, the following two prompts are valid:

PS1="(LOGNAME) "

PS1='($PW '
The first example causes your prompt to be equal to your UNIX user name.
The second example causes your prompt to be the current working directory.
9. Job control

10. Key bindings

This feature enables you to change the behavior of key combinations


for the purpose of command-line editing.

bind <key sequence> <command> // emacs

This binding is typically found in your .kshrc file, which is the startup file for the
shell (it is read whenever the shell starts).

The bind commands that are needed to create these bindings are as follows:
bind '^[['=prefix-2
bind '^XA'=up-history
bind "^XB'=down-history
bind '^XC'=forward-char
bind '^XD'=backward-char
list gives some of the most useful editing commands that you can
use for binding keys, along with the default binding

bind : get a listing of all of the editing commands that pdksh supports

abort (^G) is used to abort another editing command. It is most


commonly used to stop a history list search.

backward-char (^B) moves the cursor backward one character. This


command is often bound to the left arrow key.

backward-word (^[b) moves the cursor backward to the beginning of


a word.

beginning-of-line (^A) moves the cursor to the beginning of the


command line.
complete (^[^[) tells pdksh to try to complete the current command.

copy-last-arg (^[_) causes the last word of the previous command to


be inserted at the cursor position.
delete-char-backward (ERASE) deletes the character that is to the left
of the cursor.

delete-char-forward deletes the character to the right of the cursor.

delete-word-backward (^[ERASE) deletes the characters to the left of


the cursor back to the first white space character that is encountered.

delete-word-forward (^[ deletes the characters to the right of the


cursor up to the first character that occurs after a whitespace
character.

down-history (^N) moves down one line in the history list. This
command is often bound to the down arrow key.

end-of-line (^E) moves the cursor to the end of the current line.

forward-char (^F) moves the cursor forward one character. This


command is often bound to the right arrow key.
forward-word (^[F) moves the cursor forward to the end of
a word.

kill-line (KILL) deletes the current line.

kill-to-eol (^K) deletes all of the characters to the right of the cursor on
the current line.

list (^[?) causes pdksh to list all of the possible command names or
filenames that can complete the word in which the cursor is
currently contained.

search-history (^R) searches the history list backward for the first
command that contains the inputted characters.

transpose-chars (^T) exchanges the two characters on either side of


the cursor. If the cursor is at the end of the command line it switches
the last two characters on the line.

up-history (^P) moves up one command in the history list. This


command is often bound to the up arrow key.
Cutomization of pdksh
export ENV=$HOME/.kshrc
EDITOR=emacs
The tcsh
1.Command completion

emacs hello // this is how we search the file in current directory in tcsh

emacs /urs/bin/hello // will search this file in bin directory

// we generally do what is mentioned above


With command completion , we can do as :
2. Wildcards ( * , ? , […])
3. Command history

The shell remembers the last history commands that have been
entered into the shell (where history is a user-definable tcsh
variable).

The default filename for the history file is .history, but you can change
it using the histfile tcsh variable. This file is located in your home
directory.

(a) Use of up and down arrow keys


Commands displayed on the command line through the history list
can be edited simply by right and left arrow keys and delete and
backspace keys
The history command can be invoked by any one of three different
methods. The first method is :

history [-hr] [n]

It displays the history list to the screen.


-n:used to specify the number of commands to display.
If the n option is not used, the history command will display the entire
history list.
-h:causes history to remove the command numbers and timestamps
that are usually present in the output of the history command.
-r: tells history to display the commands in reverse order, starting with
the most recent command.
The following command displays the last five commands that were
entered:

history 5
The second method is used to modify the contents of the history file or
the history list.

history -S | -L | -M [filename]

-S : writes the history list to a file.


-L :appends a history file to the current history list.
-M :merges the contents of the history file with the current history list
and sorts the resulting list by the timestamp contained with each command.

All of the options for the second form of the history command use the
filename option as the name of the history file. If no filename is specified, the
history command will use the value of the histfile variable. If the histfile variable
isn't set, it will use the ~/.history (home directory) file.
The history command using the -c option clears the current history list.
In addition to the history command and its options, tcsh also contains
many history navigation and editing commands. The following
commands are used to navigate through the history list:
• !n re-executes the command with the history number of n.
• !-n re-executes the command that is n commands from the end of
the history list.
• !! re-executes the last command that was entered.
• !c re-executes the last command in the history list that begins with
the letter c.
• !?c? re-executes the last command in the history list that contains
the letter c.
Prompts

tcsh has three levels of user prompts.

The first-level prompt is what you see when tcsh is waiting for you to
type a command. The default prompt is the % character.This prompt
can be customized by assigning a new value to the prompt tcsh
variable:

set prompt="%t$“ // change the first-level prompt to the current time


followed by a dollar sign.

The second-level prompt is displayed when tcsh is waiting for input


when in a while or for loop The default for the second-level prompt
is %R?, where %R is a special character sequence that displays
the status of the parser. You can change the second-level prompt by
setting the value of the prompt2 tcsh variable. For example:

set prompt2="?“ //changes the second-level prompt to a question


mark.
The third-level prompt is used when tcsh displays the corrected
command line when automatic spelling correction is in effect. This
prompt is set using the prompt3 variable, and it has a default value of
CORRECT>%R (y|n|e)?.

tcsh supports special character codes in its prompt variables.


These codes are similar to the codes that bash supports in its
prompts. The main difference between the two is that the syntax for
using them is different.
tcsh prompt special character codes.

Character Code Meaning


%/ Displays the current working directory.
%h, %!, ! These codes all display the current history number.
%t, %@ These codes both display the time of day.
%n Displays the username.
%d Displays the current day of the week.
%w Displays the current month.
%y Displays the current year.

The following is an example of setting the prompt variable:

set prompt="%h %/"

This command sets the prompt to display the history number of the
current command, followed by the current working directory.
Key bindings

Like the pdksh, tcsh provides the ability to change and add key
bindings.
The tcsh implementation of key bindings is more powerful than
the way key bindings are done in pdksh.

With tcsh you can bind to things other than the built-in editor
commands.

tcsh also enables you to bind vi editing commands, whereas


pdksh only allows the binding of emacs editing commands.
Key bindings can be very useful, especially if you're using a favorite
editor other than emacs or vi. The basic syntax for defining key
bindings is

bindkey [option] <instring or keyname> <outstring or command>

The most useful editing commands you can use to bind key sequences
to, along with the default key binding for that command.

bindkey: list all the bindings that are defined in tcsh (without any
arguments)
beginning-of-line (^A): Moves the cursor to the beginning of the command
line.
backward-char (^B): Moves the cursor back one character.
end-of-line (^E): Moves the cursor to the end of the command line.
forward-char (^F): Moves the cursor forward one character.
backward-delete-char (^H): Deletes the character to the left of the cursor.
kill-line (^K): Deletes all of the characters to the right of the cursor.
clear-screen (^L): Removes all of the text from the shell window.
down-history (^N): Moves down one command in the history list.
up-history (^P): Moves up one command in the history list.
kill-whole-line (^U): Deletes all of the characters on the current line.

All of these commands are the same whether you're in emacs or vi


insert mode.

examples of setting key bindings:

bindkey ^W kill-whole-line

bindkey ^S beginning-of-line

You might also like