[Next] [Previous] [Up] [Top] [Contents]

CHAPTER 5 Shells

5.3 The Bourne Shell, sh


Sh uses the startup file .profile in your home directory. There may also be a system-wide startup file, e.g. /etc/profile. If so, the system-wide one will be sourced (executed) before your local one.

A simple .profile could be the following:

PATH=/usr/bin:/usr/ucb:/usr/local/bin:. # set the PATH

export PATH # so that PATH is available to subshells

# Set a prompt

PS1="{`hostname` `whoami`} " # set the prompt, default is "$"

# functions

ls() { /bin/ls -sbF "$@";}

ll() { ls -al "$@";}

# Set the terminal type

stty erase ^H # set Control-H to be the erase key

eval `tset -Q -s -m ':?xterm'` # prompt for the terminal type, assume xterm

#

umask 077

Whenever a # symbol is encountered the remainder of that line is treated as a comment. In the PATH variable each directory is separated by a colon (:) and the dot (.) specifies that the current directory is in your path. If the latter is not set it's a simple matter to execute a program in the current directory by typing:

./program_name

It's actually a good idea not to have dot (.) in your path, as you may inadvertently execute a program you didn't intend to when you cd to different directories.

A variable set in .profile is set only in the login shell unless you "export" it or source .profile from another shell. In the above example PATH is exported to any subshells. You can source a file with the built-in "." command of sh, i.e.:

. ./.profile

You can make your own functions. In the above example the function ll results in an "ls -al" being done on the specified files or directories.

With stty the erase character is set to Control-H (^H), which is usually the Backspace key.

The tset command prompts for the terminal type, and assumes "xterm" if we just hit <CR>. This command is run with the shell built-in, eval, which takes the result from the tset command and uses it as an argument for the shell. In this case the "-s" option to tset sets the TERM and TERMCAP variables and exports them.

The last line in the example runs the umask command with the option such that any files or directories you create will not have read/write/execute permission for group and other.

For further information about sh type "man sh" at the shell prompt.


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]