Professional Documents
Culture Documents
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.
• 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
(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 .
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.
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
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
you can redirect the output of the ls command into a file named
directory.out using the following command:
ls > directory.out
7. Pipelines
(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
2
8. Prompts
The first level is what you see when bash is waiting for a command to
be typed.
To customize your default first level prompt, set the value of the
PS1 bash variable.
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
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.
When a command is started in the foreground, it locks the shell from any
further user interaction until the command completes execution
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 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
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 +
The history command was left out of the pdksh shell because all of
its functionality could be provided by the fc command.
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 mo loads into the default editor the most recent command that starts
with the string mo.
4. Aliases
vi things-to-do-today.txt
unalias ttd
5. Input Redirection
6. Output Redirection
7. Pipelines
8. Shell Prompts
The first level is what the user sees when the shell is waiting for a
command to be typed.
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.
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
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
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.
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.
emacs hello // this is how we search the file in current directory in tcsh
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.
history 5
The second method is used to modify the contents of the history file or
the history list.
history -S | -L | -M [filename]
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
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:
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.
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.
bindkey ^W kill-whole-line
bindkey ^S beginning-of-line