You are on page 1of 5

Shell and Shell function A shell is a program that isolates the user from direct dealing with kernel

and thus with hardware. The shell program is just waiting for the user what to do next? Functions: Translation of command into a format kernel can understand and giving it to kernel for execution. It supports wild card characters. It supports input and output redirection through piping and filters. Provides shell variable and shell scripting facilities. Supports command History. Supports command aliases. Supports command line editing. Auto completions of command. Allows nclobber, ignoreeof, noglob commands. Ignoreeof when set user cn not logout using control+d Noclobber prevents overwriting of files Noglob stops interpretation of wild card characters. Usage: set o feature to set feature Usage: set +o feature to disable the feature Supports input/output redirection and shell variable and shell scripting Supports job control. Directory stacking (pushing and popping) Key bindings. Different type of shells Sh (Bourne shell) Command: sh Prompt is $ Ksh (Korn shell) Command:ksh Prompt is $ Developed by Stephen bourne in 1979 for System v Release of Unix Korn shell developed by David Korn at Bell Labs. The Korn shell builds up on functionality of the bourne shell by adding useful features like those available in c shell( job history and command aliases. Bourne shell script can be used without alteration in Korn shell The c shell was developed by Bill Joy (founder of sun micro-system) when working on a release of Berkley Unix (BSD). C shell differs in environment variable and shell script from Bourne and korn shell This shell is useful for handling multiple jobs Bourne again shell was developed by free software foundation (FSF). It includes feature of Bourne, Korn and c shell.

csh (C shell) Command: csh Prompt is % Jsh Bash (Bourne again shell) Command: bash Prompt is $

Why use more shells? The reason for the multiple shells is that Linux can handle more than one users or tasks at a time. Actually only one computer process goes on at any one moment in single process based computer system but linux can switch from one process to another giving the illusion of simultaneous action known as time sharing. The different shell provides a way to separate one users task from another users task. Different shells have different features and capability (shell environment, shell scripting, command aliases, command history). If more shells we have we can use different features of different shells. Shell treatment to Command Line When you type something using keyboard kernel collects your input and delivers it to shell. The shell obtains the command and translates into to actions and return to shell prompt. Shell is called command interpreter because it translates the command in actions. When a command is typed and followed by enter shell process uses kernel to start up a child process for corresponding command then shell waits for this child process to die (finish). Then parent shell process resumes running. Shell does following tasks in following order 1. Parsing: the shell first breaks up the command line into words, using spaces and tabs as delimiter, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space. 2. Variable evaluation: all words preceded by a $ are evaluated as variables, unless quoted or escaped. 3. Command substitution any command surrounded by back quotes is executed by the shell, which ten replaces the standard output of the command into the command line. 4. Redirection: the shell then looks for the character >, < and >> to open the files that they point to. 5. Wild card interpretation the shell finally scans the command line for wild-cards. Any word containing a wild-card is replaced by a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to command. 6. PATH evaluation it finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command. The environment
The Linux system is controlled by a number of shell variables that are separately set by the system, some during the boot sequence, and some after logging in. These variables are called system variables. You need to know the significance of many of them because they determine the environment in which you work. You can even alter their values to your liking.

(i) set statement $ set This command gives a complete list of all system variables along with their values

(ii) The PATH variable is a variable that instruct the shell about the route it should follow to locate any file. If you wish to include the directory /home/bca2 in your search list, you need to redefine this variable: $ PATH=$PATH:/home/bca2 (iii) The HOME variable is a variable that stores your home directory. Home directory is a directory where you are placed after logging. $echo $HOME this setting is stored inside /etc/passwd file. You can change the value of this variable, but it will not change the home directory as such, but only the directory that cd switches to will get changed when used without arguments. This happens because simple cd command implies cd $HOME. (iv) The IFS variable contains a string of characters that are used as word separators in the command line. The string normally consists of the space, tab and new line characters. All theses characters are invisible but the blank line following it suggests that the new line character is part of this string. $echo $IFS (v) The MAIL Variable MAIL variable determines where all incoming mail addressed to the user is to be stored. This directory is searched by the shell each time a user longs in. if any information is found there, the shell informs the user with the familiar message You have Mail. $ echo $MAIL (vi)The Variables PSI and PS2 The shell has two prompts stored in PS1 and PS2. The primary prompt string PS1 is the one you normally see ($). When you issue echo command and give string in multiple line you get prompt >. This the secondary prompt string is stored inside PS2. $ echo hello > students Normally, PS1 and PS2 are set to the characters $ ,and >, respectively. You can change the primary prompt string to C> or anything else. $ PSl="C> "

While the $ is the most commonly used primary prompt string, the system administrator uses the # as the prompt. You should avoid using this character because that can give the impression to other user that you are the system administrator.

(vii)The TERM Variable TERM indicates the terminal type being used. There are some utilities that are terminaldependent, and they require knowing the type of terminal you are using. One of them is vi editor which makes use of a control file /usr/lib/terminfo. If TERM isn't set correctly, vi won't work, and the display will be faulty. $echo $TERM (viii)The LOGNAME Variable This variable shows your username. When you wander around in the file system, you may sometimes forget your login name. (It's strange, but it happens when you have multiple accounts!) Though who am i tells you that, it also includes a number of other details that may not needed by you. $ echo $LOGNAME (ix) profile and .profile file: THE SCRIPT EXECUTED DURING LOGIN TIME .profile file is created when a user is added to the system by system administrator. It is equivalent to autoexec.bat of DOS. It contains shell script commands that are executed when user logs in. Content of .profile file can be quite large, depending on users' requirements, but an small portion of the file content is show below: $ cat .profile # User $HOME/.profile - commands executed at login time HOME=/home/bca2 PATH=$PATH:$HOME/bin:/usr/bin/Xll:/usr/hosts:/home/local/bin:. MAIL=/usr/spool/mail/$LOGNAME # mailbox location IFS= . PS1=$ PS2="> " The .profile must be located in your home directory, and it is executed after /etc/profile, the universal profile for all users. Universal environment settings are kept by the administrator in /etc/profile so that they are available to all users. (x) .login and .logout files The C shell doesn't use a .profile file for containing startup instructions; it uses two other files instead. The first file (~/.cshrc or ~/.tcshrc) contains C shell specific instructions to be

executed whenever a C shell is started. The other file (~/.login) contains only those instructions that are to be executed during login time. This file should be so designed that it can easily be converted to a .profile in case you decide to change your shell in future. You may sometimes need to place some C shell statements here too. Though the two files are meant to have distinct identities, differences can often get blurred; extensive use of the shell will tell you that. The C shell is unique in that it offers a ~/.logout script that is executed when logging out. Even though this feature can be simulated in the other two shells, it requires a separate statement to execute it.