Teach Yourself UNIX Shell Programming in 14 Days

Chapter 6: Your Environment

Objectives:

This chapter discusses standard environment variables in UNIX, the login process, the calendar program, calculator programs and subshells. Objectives important to this chapter are:

  • Use of environment variables
  • Calendar options
  • BCand dc calculators
  • invoking subshells
Concepts:

The chapter reviews several key variables in the UNIX environment:

  • HOME - the path to your home directory
  • SHELL - the default shell program for your system
  • PATH - the search paths that are examined when you tell UNIX to execute a command
  • TERM - the terminal type you are using
  • USER - the variable that holds your login ID (LOGNAME is an alternative)
  • PWD - variable that remembers your present working directory
  • IFS - interfield separator; this is set to a whitespace
  • MAIL - location for your mailbox on the system

When you log into a UNIX system, UNIX verifies your ID and password, then starts a shell. Before you get a prompt, two programs are run:

  • /etc/profile
  • /$HOME/.profile

The first is a general program for all users, and the second is specific to you, being in your home directory. They are probably run as arguments to the period command. This command makes certain that the programs run in the current shell, and not in a subshell. As an example, this command does just that:

	. /etc/profile

This is done so that the shell you log into receives the variable settings from these programs, not some subshell. More on that in a minute.

PS1 and PS2 are actually shell prompts 1 and 2. (Misnamed, aren't they?) The first one is the prompt you see at the beginning of a command line. This is probably the dollar sign. The second one is the prompt you see when you press enter, but have not finished the command (such as "cat > newfile" ).

UNIX includes an extensive calendar program. It is useful for putting a small calendar page on the screen, for any month or year you are likely to want to see.

The date function in UNIX will report the current date and time. It can also be used to calculate dates and times based on a given date (such as the system date.)

There are two commonly distributed calculators in UNIX that may be available on your system. They are called bc and dc. The BC calculator is the more commonly distributed one, and for good reason: it is the one that uses algebraic entry. Your book refers to this entry method as the infix method. The dc calculator uses what your text refers to as the postfix entry method. You may be more familiar with this method by the name reverse Polish notation. If you have both programs on your system, feel free to use whichever you like better. If you have only one, it is most likely the BC calculator.

Now, I promised to talk about subshells. A subshell is a shell that you call into existencefrom another shell. This is the most common way to run a script. The subshell may be thought of as the called shell, or the child shell. The originating shell may be thought of as the calling shell, or the parent shell. Like c functions, shells have local variables that are private. Subshells have no knowledge of the variables in their parent unless the parent shell invokes the export command. This command is followed by a variable name, and that variable is then available to any subsequently called subshell.

On a command line, subshells can be invoked by using parentheses. Place the parentheses around commands that you want executed in a subshell. You can use multiple commands, separating them with semicolons. You can also group commands together and execute them in the current shell using curly braces. Each separate command in the curly braces should have a terminating semicolon. Examples:

	(cd /etc; ls)
	{CD /bin; Ls;}

Note the single semicolon separating two commands in the parentheses, and the trailing semicolon after each command in the curly braces. It may be useful to execute commands in a subshell when you do not want the aftereffects (such as placing you in a strange directory) to persist after the sequence of commands in the parentheses are completed.