|Previous||Table of Contents||Next|
Environment variables are often used to define initialization options such as the default login shell, user login name, search path, and terminal settings. Some environment variables are set for you each time you log in. You can also create your own variables and assign values to them. By convention, environment variable names are in capital letters.
Environment variables are passed on from parent to child processes. For example, an environment variable set in a shell is available to any program started in that shell, to any additional program started by the initial program, and so on. In other words, environment variables are inherited from parent to child, from child to grandchild, and so on. They are not inherited backward from parent to grandparent or child to parent.
Use the env command to display a list of current environment variables. Consult the Solaris System Administrator's Guide for more information about environment variables.
For the Bourne and Korn shells, use the following syntax to assign environment variables and export them. You must export the variable before it can be put into the environment of child processes.
For the Korn shell, you can also use the following syntax:
For the C shell, use the setenv command with the following syntax to assign environment variables:
setenv VARIABLE value
Use the unsetenv command with the following syntax to remove the environment variable:
When you write shell scripts, you want to be able to obtain input from sources outside your scripts; for example, from another file or from keyboard input. You also want to be able to generate output both for use within the script and for display on-screen.
The following sections describe how to control input and output of a shell script using standard input, output, error, and redirection; how to accept user input (input from the keyboard) to a script; how to create "here" documents; and how to generate output to the screen.
When writing shell scripts, you can control input/output redirection. Input redirection is the ability to force a command to read any necessary input from a file instead of from the keyboard. Output redirection is the ability to send the output from a command into a file or pipe instead of to the screen.
Each process created by a shell script begins with three file descriptors associated with it, as shown in Figure 16-1.
Figure 16-1 File descriptors.
These file descriptorsstandard input, standard output, and standard errordetermine where input to the process comes from, and where the output and error messages are sent.
Standard input (STDIN) is always file descriptor 0. Standard input is the place where the shell looks for its input data. Usually data for standard input comes from the keyboard. You can specify standard input to come from another source using input/output redirection.
Standard output (STDOUT) is always file descriptor 1. Standard output (default) is the place where the results of the execution of the program are sent. Usually, the results of program execution are displayed on the terminal screen. You can redirect standard output to a file, or suppress it completely by redirecting it to /dev/null.
Standard error (STDERR) is always file descriptor 2. Standard error is the place where error messages are sent as they are generated during command processing. Usually, error messages are displayed on the terminal screen. You can redirect standard error to a file, or suppress it completely by redirecting it to /dev/null.
You can use the file descriptor numbers 0 (standard input), 1 (standard output), and 2 (standard error) together with the redirection metacharacters to control input and output in the Bourne and Korn shells. Table 16-7 shows the common ways you can redirect file descriptors.
|Take STDIN from file||<file, or 0<file|
|Redirect STDOUT to file||> file, or 1>file
||Redirect STDERR to file
||Append STDOUT to end of file
||Redirect STDERR to STDOUT
||Pipe standard output of cmd1 as standard input to cmd2
||cmd1 | cmd2
||Use file as both STDIN and STDOUT
When redirecting STDIN and STDOUT in the Bourne and Korn shells, you can omit the file descriptors 0 and 1 from the redirection symbols. You must always use the file descriptor 2 with the redirection symbol.
The 0 and 1 file descriptors are implied, and not used explicitly for the C shell, as shown in Table 16-8. The C shell representation for standard error (2) is an ampersand (&). STDERR can only be redirected when redirecting STDOUT.
|Redirect STDOUT to file||> file|
|Take input from file||< file|
|Append STDOUT to end of file||>> file|
|Redirect STDOUT and STDERR to file||>& file|
|Append STDOUT and STDERR to file||>>& file|
|Previous||Table of Contents||Next|