View
102
Download
0
Category
Tags:
Preview:
Citation preview
Linux Shell Environment
Content Updating Program(CUP)
On
Linux Programming
Shell OverviewWhat is a Shell?
Linux shells provide a "command line" interface which allows the user to enter commands.
A shell is a program which reads and executes commands for the user.
Shell also usually provide features such as Job Control, Input/output Redirection and a Command Language for writing Shell Scripts.
The user can pick their shell (just like the applications, desktop manger, window manager, etc. on a LINUX system).
On the UMBC GL network, the default UNIX shell is tcsh -Turbo C Shell.
Shell Overview Shells available include:
tcsh - Turbo C SHell
csh - C SHell
ksh - Korn SHell
bash - Bourne Again SHell
sh - SHell
Linux Default Shell
Most Linux systems (especially home installations) default to
the bash shell.
Changing Your Shell - On a Home Based System
Usually there is a command called chsh that stands for
change shell.
You have to enter your password and then the absolute path
to the new shell that you wish to use.
Shell Environment Variables The bash shell uses a feature called ‘environment
variables’ to store information about the shell session
and the working environment.
USE: - Many programs and scripts uses environment
variables
- to obtain system information
- to store temporary data
- configuration information.
There are two types of environment variables
- Local Environment Variables
- Global Environment Variables
Gobal Environment Variables
Global Env. Variables(GEV) are visible from
the shell session and from any child process that
the shell spawns.
Linux system sets several GEV on start of Shell.
System environment variables uses CAPITAL
letter to differ from user environment variables.
To view the GEV, use command
$ printenv
GEV contiuned…..
To display a value of individual env variable
Use command
$ echo $HOME
Example of GEV available to Child process also…
Local Environment Variables
LEV can be seen in the local processes in which
they are defined.
Linux system also define standard LEV for user
by default.
To view LEV use the command
$ set
(additional variables not in printenv are LEV)
SETTING Env Variables
Setting LEV: Users are allowed to create local
variables that are visible within user shell process
We can assign either numerical or string value to
and environment variable using equal sign (=)
Example:
$ test=testing
$ echo $test
Standard Convention is use Lower case for LEV
and user Upper Case for GEV.
SETTING GEV continued… SETTING GEV:
Create a LEV and export it to global env.
Example:
$ test=‘testing your patience’
$ echo $test
$ export test
$ bash
$ echo $test
???
Removing Env. Variables Remove the Environment variable using
command – unset
Example:
$ unset test (don’t use $before variable..)
$ echo $test
Example for GEV…
Default Shell Env. Variables
List of Default Variables…
Important Environment Variables
HOME - your home directory.
USER and LOGNAME - your login ID.
HOSTNAME - the name of the host computer.
PWD - the current working directory.
MAIL - where your mail is located.
PATH - a list of directories in which to look for executable commands.
Certain applications and commands may communicate with the shell and reference the environment variables that it maintains. For example, it seems that frm and nfrm seem not to work if $MAIL is
not defined. frm and nfrm are commands to list the contents of your inbox without logging into pine.
Setting the PATH env. variable PATH env. Variable includes all of the directories
where your applications resides.
We can add new search directories to existing
PATH environment variable without rebuild it
from scratch.
Example:
$ echo $PATH
---------------
$ PATH=$PATH:/home/user
$ echo $PATH
Locating System Env. variables
When we start a bash shell by logging on to the
system, it searches several files for command,
These file are called Startup files.
Following is the order in which bash processes these
files:
/etc/profile
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
Variable Arrays Environment variables can be used as arrays.
So it can hold multiple values.
To set list them in parentheses separated by
comma.
Example:
$ mytest=( one two three four five )
$ echo $mytest
????
$ echo ${mytest[2]}
???
Aliasing Commands A helpful feature, especially for many users new to LINUX, is
the alias function.
The alias command assigns a command, possibly with many options and flags, to another name. Usually it is a shorter name or one that is easier to remember.
Setting up an alias:
The exact syntax depends on the shell that you are using. We will cover how to do it under tcsh and bash. Most other shells use a similar or identical syntax.
tcsh syntax: alias <aliased name> <original command>
bash syntax: alias <aliased name>=<original command>
Example:
$ alias dir=‘ls –l’
$dir
Thank you …
Recommended