0% found this document useful (0 votes)
22 views23 pages

OS Lab 2 - Workshop2

The document discusses the shell environment in Linux, including different shell types, shell configuration files, command line input, executing programs, aliases, environment variables, special characters, and redirecting output.

Uploaded by

abdul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views23 pages

OS Lab 2 - Workshop2

The document discusses the shell environment in Linux, including different shell types, shell configuration files, command line input, executing programs, aliases, environment variables, special characters, and redirecting output.

Uploaded by

abdul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lab02 – Part II

The Shell Environment


PTA Linux Series

Professional Training Academy Linux Series

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Types

• There are many types of shells available on Linux. Most of


which have a history from UNIX

• To check which types of shells are available on your system,


type:

cat /etc/shells

– Examples include: bash, sh, ash, bsh, tcsh,


PTA Linux Series

csh, ksh, and zsh

– You can switch shell by typing it's name. Although there


would be very few reasons for you to need to do this

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Types

• The most popular shells are:


– bash: Bourne Again Shell (we are using this shell)
– tcsh: TC Shell
– ksh: Korn Shell

• The shell is responsible for:


– Parsing the command line and input
PTA Linux Series

– Executing programs
– Evaluating special characters, e.g. *
– Managing pipes, redirection, signals, and background
processes

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Configuration Files

• The bash shell looks for a series of configuration files when


starting up
PTA Linux Series

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Configuration Files

• The system wide configuration file is in /etc/profile

• As a user, you can customise your own shell using one of the
hidden configuration files in your home directory, for
example, ~/.profile

• Note: Hidden files are any files


beginning with a full stop .*
PTA Linux Series

• These files are the place to store your environmental variables


(explained later) or any other shell customisations you wish to
make

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Command Line Input
• If you have typed in a long command and notice a typo at the
start of the line, you may use ctrl-a to return to the start of
the line

• Likewise, you may use ctrl-e to return to the end of the


line

• The shell also supports auto-completion of command and file


names
PTA Linux Series

– Type in the first few letters of a command and use tab to


auto-complete the name

– If there are multiple commands beginning with these


letters, the shell will print back a list which you can
choose from

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Command Line Input
• The shell also records your command history

• You can access your previous commands using the up


arrow and the down arrow

• A useful shortcut is to use the exclamation mark which looks


up matches of your previous commands

• For example, if you had previously edited a file as follows:


PTA Linux Series

emacs reallylongfilename, you could use the


following shortcut:

!emacs r

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Executing Programs
• If you execute a program, such as emacs from your shell, you
will notice that you cannot type anymore commands until
emacs is finished

• To avoid this, you can tell the shell to run any program such
as emacs as a background process using the ampersand sign &

emacs&
PTA Linux Series

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Executing Programs in your PATH

• The bash shell uses the $PATH environment variable to locate


commands

• If you cannot run a program, you can echo the PATH variable
as a check, for example:

echo $PATH
PTA Linux Series

• To add a directory to your path, separate it by a colon, for


example:

export PATH=$PATH:/my/cpp/programs:

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Creating an Alias for your Programs
• The bash shell allows you to create an alias or a shortcut
name for programs

• The command alias will return a list of defined aliases in


your shell

• Aliases can be useful for shortcuts, typos, and adding extra


options, for example:
PTA Linux Series

alias e=emacs

alias emcas=emacs

alias ls=‘ls -lh’

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Executing Programs with
Environment Variables
• Environment variables can be set from the terminal or the shell
configuration file

• For example, to set-up your proxy in the bash shell, type:

export http_proxy=http://student-proxy.ul.ie

• The export command sets the value of environment variables


PTA Linux Series

• You will need to put this line into your shell configuration file
to make it more permanent, e.g. ~/.profile

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Executing Programs with
Environment Variables
• You can check the value of a variable using the echo
command and placing a dollar sign before the variable, for
example:

echo $http_proxy

• You can list the complete set of environment variables using


the printenv or the env command
PTA Linux Series

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Special Characters
• Special characters will be evaluated by the shell

• For example, if you want to search all files in your current


directory for the word “password”, type:

grep password *
PTA Linux Series

• The wildcard character (*) is evaluated by the shell and


expanded to mean all file occurrences found

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Special Characters
• Other special characters can be combined to form regular expressions.
Examples of frequently used special characters include:

• . : Match a single character

grep ‘c.e’ filename

• ^ : Beginning of line

grep ‘^coffee’ filename


PTA Linux Series

• $ : End of line

grep ‘coffee$’ filename

• \w : Alphanumeric character [a-zA-Z0-9]

grep ‘c\w*e’ filename \w* matches 0 or more

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Redirecting Output
• The shell allows us to redirect the output of one command to
the input of another command

• For example, if we do ls in a directory with hundreds of files


it is difficult to keep track of all the files. However, the shell
will let us take the output of the ls command and send it to a
more useful viewing command such as less

• We can link the two commands using the pipe operator as


PTA Linux Series

follows:

ls | more

• The output of the command on the left becomes the input for
the command on the right hand side

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Redirecting Output
• The shell will also allow us to redirect the output to a file,
which can be useful for creating log files

– e.g. to save all the listed files to a logfile, type:

ls > logfile

• A double arrow operator can also be used for appending, e.g.


PTA Linux Series

ls >> logfile

• Input can be taken from sources using the opposite arrow, e.g.

mail [email protected] < test_results

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Customisation – Prompt
Messages
• In order to customise your shell, you need to alter the Prompt
String 1 environment variable, which is called PS1
• For example, if you wish to only have a right arrow for your
prompt, type:

export PS1=“>”

• Or any other message:


PTA Linux Series

export PS1=“beware of my shell >”

• Special characters can also be used, for example:


– \t : current time in HH:MM:SS
– \w : current working directory
– \u : the current user
– \h : the hostname
– \a : the ASCII bell character

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Customisation – Colouring
Prompts
• To add colour to your prompt, we add a numeric colour value
between \e[ and m. Numeric codes are separated by
semicolons if more than one is needed

• You can control the background, foreground and bold


settings in your terminal

• Note: You will need to reset the colour


PTA Linux Series

codes at the end of your prompt to avoid


the colour being applied to typed text.
The numeric code 0 is used to reset the
background, foreground and bold settings,
as follows: \e[0m

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell– Colouring Customisation
Prompts
• The following chart can be used to choose your colour and
bold settings:
PTA Linux Series

Source: IBM Developer Works: www-128.ibm.com/developerworks/linux/library/l-tip-prompt

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Customisation - Colour Prompts
• The rows indicate text colour, while the columns indicate
background colour
• For example, to set your text to be white on a blue
background, we need numeric colour codes: 37 for white text
and 44 for blue background (\e[37;44m). We will apply
these settings to the current user, time, and directory with a
dollar sign for the end of our prompt (\u\t\w$)

export PS1=“\e[37;44m\u\t\w$”
PTA Linux Series

• Unless we want to keep this colour scheme for our typed in


text, we will also need to reset our terminal (\e[0m)

export PS1=“\e[37;44m\u\t\w$ \e[0m”

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Shell Customisation – Finishing
Touches to Our Colour Prompt
• In order to make the text more readable, we can set the bold
face by adding a 1 as follows:

export PS1=“\e[37;1;44m\u\t\w$ \e[0m”

• Note: When we customised our prompt word-


wrapping was turned off, in order to fix
this we need to escape all non-printable
characters so that long commands of text
are wrapped onto the next line again. The
PTA Linux Series

bash escape sequences for this are \


[ and \]

export PS1=“\[\e[37;1;44m\]\u\t\w$ \[\e[0m\]”

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Exercises
1) Change into your project1 directory inside your home directory,
re-edit your report.txt file using the least amount of keystrokes
possible
2) Check to see which directories are searched for programs that you
type in
3) Create a shorter alias for emacs
4) Create a file called shopping_list with the following 3 lines:

bread, butter, tea, biscuits


PTA Linux Series

apples, 6 oranges, strawberries


chocolate, ice-cream, 1 cake


Now, search for a line beginning with “apples”

Search for any line which contains the letters “ea”

Search for a line ending with “s”

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©


Exercises
5) Save the output of ls to a log file for later use
6) Customise your terminal to have green text on a black background
with the current time and current directory as the prompt
7) Edit your bash configuration file in your home directory to save your
settings for your customised prompt
8) Edit your $PATH shell variable so that when when you issue a
command from the prompt, the shell goes to look for a program of
that name by first looking in your current directory. This means
modifying the shell variable called $PATH so that it starts with '.:'.
The colon is used as a separator and the dot is shorthand for “current
directory.” First you will need to find what shell config file the
PTA Linux Series

$PATH variable is defined in!


This exercise is useful because it means that instead of having to type
'./myprog' to run the program of that name in the current directory
you can now simply type 'myprog'. Doesn't seem like much now but
wait until your back is to the wall and you're tearing your hair out
trying to get an assignment working...

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

You might also like