You are on page 1of 13

Shell

The shell is the command prompt within Linux where you can type
commands. If you have logged into a machine over a network (using ssh or
telnet) then the commands you entered were run by the shell. It is in fact a
program that is running on top of the operating system.
To take a basic view of how Linux is built up see the diagram below:

The different layers of the Linux operating system


The kernel is the heart of the operating system. This is the bit that is
actually Linux. The kernel is a process that runs continuously managing the
computer. The kernel is a very specific task so to allow programs to
communicate with it there are a number of low level utilities that provide an
interface between the application and the kernel.
The shell is an application that allows users to communicate with the
computer. It is a text based application that allows programs to be started
and tasks to be run. The shell is within a collections of utilities known as
GNU. Without the kernel the computer cannot run and without the GNU
utilities it can't do anything useful which is why the operating system is
sometimes called GNU/Linux.

The Different Shells


In the same way that different variants of UNIX were developed there are
also different variants of the shell.
Here's a list of the most common UNIX shells:

Name of Command
shell
name
Bourne
sh
shell
Korn
ksh / pdksh
Shell
C Shell

csh

Bash
Shell

bash

tcsh

tcsh

Description
The most basic shell available on all UNIX
systems
Based on the Bourne shell with
enhancements
Similar to the C programming language in
syntax
Bourne Again Shell combines the advantages
of the Korn Shell and the C Shell. The
default on most Linux distributions.
Similar to the C Shell

Common Linux / UNIX shells


When you login to a Linux machine (or open a shell window) you will
normally be in the bash shell.
You can change shell temporarily by running the appropriate shell command.
To change your shell for future logins then you can use the change shell
(chsh)command. This is normally setup to only allow you to change to one of
the approved shells listed in the /etc/shells file.
The shell is more than just a way of typing commands. It can be used to
stop, start, suspend programs and by writing script files it becomes a
programming language in itself.

details of the shells are listed below.


Bourne Shell - This is the oldest shell and as such is not as feature rich

as many of the other shells. It's feature set is sufficient for most
programming needs however it does not have some of the user
conveniences that are liked on the command line. There is no option to reedit previous commands or to control background jobs. As the bourne shell
is available on all UNIX systems it is often used for programming script files

as it offers maximum portability between different UNIX versions. Bash is


fully backwards compatible with the Bourne Shell so running the bourne shell
on Linux will often call the bash shell (using a link between the files).
Korn Shell - This is based on the Bourne shell. One enhancement that is
particularly useful is its command-line editing facility. It is possible using
either vi or emacs keys to recall and edit previous commands. This is not as
easy to use as some of the other shells, but work well across a network or
using a physical terminal (rare these days). It also has more powerful
programming constructs than the bourne shell, however these are not as
portable. To run the Korn shell you can run either ksh or pdksh from the
normal shell (assuming it is installed).
C Shell - The c shell syntax is taken from the C programming language. As
such it is a useful tool for anyone familiar with programming C.
Bash Shell - The Bash shell is a combination of features from the Bourne
Shell and the C Shell. It's name comes from the Bourne Again SHell. It has a
command-line editor that allows the use of the cursor keys in a more "user
friendly" manner than the Korn shell. It also has a useful help facility
allowing you to get a list of commands by typing the first few letters followed
by the "TAB" key. It is the default shell on most Linux distributions and
unless otherwise specified is the shell used for the future examples.
tcsh - This is a different shell that emulates the C Shell. It has a number of
enhancements and further features even than the bash shell.

The Shell Prompt


When logged into the shell you will normal see one of the following
prompts: $, % or #. This is an indication that the shell is waiting for an input
from the user. The prompts can be customised but generally the last
character should be left as the default prompt character as it helps to
indicate which shell you are running and whether or not you are logged in as
root.
The Bourne, Korn, and Bash shells all use a similar syntax. Unless you are
using one of the advanced features you do not necessarily need to know
which one of them you are in. If however you are in the C or tcsh shells this
uses a completely different syntax and can require commands to be entered
differently. To make it a little easier these have two different prompts
depending upon the shell.
The default prompts are:
$ - Bourne, Korn and Bash Shells
% - C Shell

Login Settings for the Bash Shell


When you login to a shell a number of variables and settings are configured
for your shell. The files that are most commonly used by bash are:
1.
2.
3.
4.

/etc/profile
~/.bash_profile
~/.bashrc
~/.bash_logout
These files are text based shell scripts that can be used to define settings for
either system wide settings (those in the /etc directory), or for an individual
user (those in the users home directory specified by ~). Different files are
called depending upon whether it is an interactive login shell or a noninteractive shell.

Bash as an Internactive Login Shell


The following is followed if bash is invoked as an interactive login shell, or as
a non-interactive shell with the --login option.
First the shell reads and executes commands from the file '/etc/profile', if
that file exists. After reading that file, it looks for '~/.bash_profile'. If this is
not found then it can instead use '~/.bash_login', or '~/.profile'. The `-noprofile' option may be used when the shell is started to inhibit this
behavior.
The .bash_profile file is normally configured so that it also calls the
~/.bashrc file (if it exists) towards the end of the .bash_profile.
When the login shell exits, Bash reads and executes commands from the file
`~/.bash_logout', if it exists.

/etc/profile
The /etc/profile file provides the system wide default environment variables.
Typically this sets up the umask, LOGNAME, and mail directories etc. It can
also be used to change the default command search path (PATH) for all
users on the system. As most systems don't have a /etc/bashrc file aliases
are sometimes included in the /etc/profile file.

~/.bash_profile
This provides the user specific environment variables, and is often used to
add local search paths onto the PATH. This is called after the /etc/profile
script.

~/.bashrc
This file is called for non-interactive shells, and is normally called from the
~/.bash_profile for interactive shells. It is normally used for setting up
aliases and any other commands that are run during the startup.

~/.bash_logout
The ~/.bash_logout script is called when the user logs out of the interactive
shell.

The vi Editor
Introduction:
vi is a powerful editor with many features. It can handle very large files
much easier than a program like Microsoft Word. Unlike Word, vi is only a
TEXT EDITOR and you cannot include graphics or fancy fonts in your file
the vi editor typically functions in three different modes:
1. Command mode
2. Insert mode
3. colon mode

1) vi command mode
When you first start editing a file with the vi editor you will be in vi
command mode. In this mode you can issue many vi commands, including
commands like insert, append, and delete, and other search and navigation
commands that let you move around your file.
Possibly the most important thing to know is that when you're in command
mode you can't insert text immediately. You first need to issue
an insert,append, or open command to insert text.

2) vi insert mode
Once you issue a vi insert, append, or open command, you will be in vi
insert mode. If you're working with a modern vi or vim implementation, your
vi editor is typically configured to show the current mode of operation, so
when you go into insert mode, you'll see a text string like this on the last
line of your vi editor window:
-- INSERT At this point you can (a) type text into your file and (b) use the arrow keys
to navigate around your file just as you would do with any other text editor.
A very important concept to know is that when you're in vi insert mode, but
you want to switch back to vi command mode, you easily move back to
command mode by pressing the [Esc] key.

3) vi colon mode
The last vi mode is known as vi colon mode. You can only get to colon mode
from command mode, and you get into last line mode by pressing the colon
key, like this:
press {esc} and then press{ :}colon
After pressing this key, you'll see a colon character appear at the beginning
of the last line of your vi editor window, and your cursor will be moved to
that position. This indicates that vi is ready for you to type in a "last line
command".
From this vi command prompt you can do all sorts of really amazing things.
You can do simple things, like quitting your vi session, like this:
:q means quit
:q!--> means quit forcely
:wq means save and quit

Using Vi
vi is an interactive text editor which is display-oriented: the screen of
your terminal acts as a window into the file you are editing. Changes you
make to the file are reflected in what you see.
Using vi you can insert text anywhere in the file very easily. Most of
the vi commands move the cursor around in the file. You can move the
cursor forward and backward in units of characters, words, sentences, and
paragraphs.

The vi Editor
Introduction:
vi is a powerful editor with many features. It can handle very large files
much easier than a program like Microsoft Word. Unlike Word, vi is only a
TEXT EDITOR and you cannot include graphics or fancy fonts in your file
the vi editor typically functions in three different modes:
1. Command mode
2. Insert mode
3. colon mode

1) vi command mode
When you first start editing a file with the vi editor you will be in vi
command mode. In this mode you can issue many vi commands, including

commands like insert, append, and delete, and other search and navigation
commands that let you move around your file.
Possibly the most important thing to know is that when you're in command
mode you can't insert text immediately. You first need to issue
an insert,append, or open command to insert text.

2) vi insert mode
Once you issue a vi insert, append, or open command, you will be in vi
insert mode. If you're working with a modern vi or vim implementation, your
vi editor is typically configured to show the current mode of operation, so
when you go into insert mode, you'll see a text string like this on the last
line of your vi editor window:
-- INSERT At this point you can (a) type text into your file and (b) use the arrow keys
to navigate around your file just as you would do with any other text editor.
A very important concept to know is that when you're in vi insert mode, but
you want to switch back to vi command mode, you easily move back to
command mode by pressing the [Esc] key.

3) vi colon mode
The last vi mode is known as vi colon mode. You can only get to colon mode
from command mode, and you get into last line mode by pressing the colon
key, like this:
press {esc} and then press{ :}colon
After pressing this key, you'll see a colon character appear at the beginning
of the last line of your vi editor window, and your cursor will be moved to
that position. This indicates that vi is ready for you to type in a "last line
command".
From this vi command prompt you can do all sorts of really amazing things.
You can do simple things, like quitting your vi session, like this:
:q means quit
:q!--> means quit forcely
:wq means save and quit

Editing A File
The most common way to start a vi session is to tell it which file to edit. To
edit a file named filename, use the command:
Example: vi filename
The screen will clear and the text of your file will appear on the screen. If
filename doesn't exist yet, vi will start you in a new file, and when you tell it
to save your work, it will use the filename that you specified.
Arrow Keys
On most terminals, you can use the arrow keys to move the cursor around.
Left and right moves the cursor left or right one character, and up and down
move the cursor up or down one line. The other way to move the cursor is
with the h, j, k, and l keys
h

left

down

up

right

Searching
Another way to position yourself in the file is by giving the editor a string to
search for. If you type "/" followed by a string of characters and press Enter,
the editor will search for the next occurrence of this string in your file, and
place the cursor there. Pressing n will go to the next occurrence after that.
If instead of forward, you want to search backward for a string, use ?
instead of /. In a ? string search, pressing n will take you to successive
occurrences in the same (backward) direction.
Example: /abc
Output: searches and highlights occurrence of abc in text .
Other Options:
SPACE

advance the cursor one position

repeats the last change command

opens and inputs a new line, above the current line

opens and inputs new lines, below the current line

undoes the changes you made to the current line

appends text after the cursor

appends text at the end od the line

deletes the object that you specify

dd

deletes a line

4dd

deletes 4 line from cursor line

inserts text before the cursor

inserts text at the start of line

deletes a char on which cursor is placed

undoes the last change

echo
echo displays a line of text.

Overview
echo is a fundamental command found in mostoperating systems that offer
a command line. It is frequently used in scripts, batch files, and as part of
individual commands; anywhere you may need to insert text.

Syntax: echo [options] string.


Example: echo abc
Output: abc will be displayed on terminal.
Options:
\b

Backspace.

\c

Produce no further output after this.

\e

The escape character; equivalent to pressing the escape key.

\f

A form feed.

\n

A newline.

\r

A carriage return.

\t

A horizontal tab.

\v

A vertical tab.

echo examples

Example1: echo Hello, World!


Output: Hello, world!
example2: x=10
echo The value of x is $x.
Output: The value of x is 10.
Example3: echo -e 'Here \bthe \bspaces \bare \bbackspaced.'
Output: Herethespacesarebackspaced.
Example4: echo -e 'Here\nwe\nhave\ninserted\nnewlines.'
Output:

Here
we
have

inserted
newlines.
Example5: echo -e 'Here\twe\thave\tinserted\thorizontal\ttabs.'
Output: Here

we

have

inserted

horizontal

tabs.

Example6: echo -e 'This line is not completely \cprinted.'


Output: This line is not completely
Example7: echo 'This text is now in a text file.' > textfile.txt
Output: Writes the text This text is now in a text file. to the file textfile.txt.
If textfile.txt does not exist, it will be created; if textfile.txt already exists, it
will be overwritten.
Example8: echo 'This text is now in a text file.' >> textfile.txt
Output: Appends the text This text is now in a text file. to the file
textfile.txt. If textfile.txt does not exist, it will be created.
Example9: echo *
Output: Output a string comprising the name of each file in the working
directory, with each name separated by a space. For example, if you have
three files in your working directory, output may resemble the following:
flower.jpg document.doc readme.txt

You might also like