Unix Tutorial


What is Unix?

Getting started

Hardware

Software

Basic Commands

vi Commands

 

Files

File Commands

Different types of file

File Redirection

Permissions

Pipelines / Filters

 

Directory

Directory Structure

Directory Command

Shell

Different types of shell

 

Compiling Program

Networking

Commands

Reference

Reference Commands

Online help:Manpages

Useful links/Books

 

Search Engine

The Shell


Whenever you login to a Unix system you are placed in a program called the shell. You can see its prompt at the bottom left of your screen.(i.e Paris).To get your work done, you enter commands at this prompt. The shell acts as a command interpreter; it takes each command and passes it to the operating system kernel to be acted upon. It then displays the results of this operation on your screen.

Following are few of the features of the shell :

Create an enviornment

Working environment is defined whenever you login or start another shell. This environment is set using the values that the shell finds in initialisation files which it always reads as it starts up. You can change your working environment by editing these files and setting new values for variables. Each shell handles its initialisation files in a different way.

Frequently used Environment Variables

Name

Description

EDITOR

Sets the editor that will be used by other programs such as the mail program.

PATH

Specifies the directories that the shell is to look through to find a command. These directories are searched in the order in which they appear.

HOME

The HOME variable contains the name of your home directory. When you issue the cd command with no directory argument, you will be placed in the directory defined in the HOME environment variable. The HOME variable is also where the shell will look for the user login scripts

PRINTER

Sets the printer to which all output is sent by the lpr command

SHELL

Sets your default shell.

MAIL

The MAIL variable contains the name of the directory where your incoming mail is stored. When you start a mail program, the program will look in the directory stored in the MAIL environment variable for your incoming mail messages.

TERM

Sets your terminal type for programs such as the editor and pager.

USER

The USER variable contains your username. Any time you access a file or directory, the access permissions are checked against the value of USER

TZ

Sets the time zone you are in.

 


Finding out which shell you are using

Information about which shell you are using is held in the SHELL environment variable.The command

echo $SHELL

displays the value of this variable. You can identify which shell you are presently using from the last part of the pathname.

Pathname

Shell

/.../sh

Bourne shell

/.../csh

C shell

/.../tcsh

TC shell

/.../ksh

Korn shell

/.../bash

Bourne Again shell

To view all the environment variables,use the command printenv

 


Changing your environment :Bourne shell

At login the Bourne shell reads the initialisation files /etc/profile and $HOME/.profile. You cannot change the content of /etc/profile, but you have permission to edit the contents of .profile, which is in your home directory and is owned by you.

Useful tip : Make a copy of your shell start up file before editing it to change environment or shell variables. You can always return to using this copy if you find that the modified file is not working and you are unable to fix the problem.

For example:

cd

cp .profile old_profile

pico .profile

The first command changes you to your home directory, where a copy of the file .profile is made in old_profile. You can then edit the .profile.

To revert to using the original .profile you simply copy it back. For example:

cp old_profile .profile

Displaying current variables

You can use the echo command to display the value of an environment variable. For example:

echo $PATH

This displays the value of the environment variable PATH. The name of an environment variable is given in UPPER CASE. The $ sign is a shell metacharacter that uses the value of the variable instead of its name. The method for displaying the value of every variable varies according to which shell you are using.

For the Bourne shell use the command set to display the value of environment variables.

For the C and TC shell use the command printenv or env to display the value of environment variables and the command set to display the value of local shell variables.

If the list of variables is so long that it scrolls off the screen and you want to display the information one screenful at a time, pipe the output through a pager. For example:

set | more


Setting environment and shell variables

csh

To set an environment variable in csh, use the setenv command. The command has the syntax: setenv VARIABLE value. To set the EDITOR variable to the value emacs in csh, use the command: setenv EDITOR emacs.

sh, or ksh

To set an environment variable in sh or ksh, use the syntax VAR=value;export VAR, where VAR is the name of the environment variable and value is the value you wish to assign. Do not put spaces on either side of the equals sign. The export command instructs the shell to propagate the value of the variable to all programs that are run by the shell. If an environment variable is reset, but not exported, the change will only apply to the shell itself. To set the EDITOR variable to the value emacs in ksh or sh, use the command:

EDITOR=emacs;export EDITOR

It is also possible to unset environment variables, with the unset command. Unsetting an environment variable removes the definition of the variable.