The Bourne shell permits you to do variable and file name substitutions.
The following sections offer information about creating and substituting variables in the Bourne shell:
The Bourne shell has several mechanisms for creating variables (assigning a string value to a name). Certain variables, positional parameters and keyword parameters, are normally set only on a command line. Other variables are simply names to which you or the shell can assign string values.
The shell recognizes alphanumeric variables to which string values can be assigned. To assign a string value to a name, enter:
Name=String
A name is a sequence of letters, digits, and underscores that begins with an underscore or a letter. To use the value that you have assigned to a variable, add a $ (dollar sign) to the beginning of its name. Thus, the $Name variable yields the value specified by the String variable. Note that no spaces are on either side of the = (equal sign) in an assignment statement. (Positional parameters cannot appear in an assignment statement. They can only be set as described in Positional Parameters .) You can put more than one assignment on a command line, but remember that the shell performs the assignments from right to left.
If you enclose the String variable with " or ' (double or single quotation marks), the shell does not treat blanks, tabs, semicolons, and new-line characters within the string as word delimiters, but imbeds them literally in the string.
If you enclose the String variable with " (double quotation marks), the shell still recognizes variable names in the string and performs variable substitution; that is, it replaces references to positional parameters and other variable names that are prefaced by $ (dollar sign) with their corresponding values, if any. The shell also performs command substitution within strings that are enclosed in double quotation marks.
If you enclose the String variable with ' (single quotation marks), the shell does not substitute variables or commands within the string. The following sequence illustrates this difference:
You: num=875 number1="Add $num" number2='Add $num' echo $number1 System: Add 875 You: echo $number2 System: Add $num
The shell does not reinterpret blanks in assignments after variable substitution. Thus, the following assignments result in $first and $second having the same value:
first='a string with embedded blanks' second=$first
When you reference a variable, you can enclose the variable name (or the digit designating a positional parameter) in { } (braces) to delimit the variable name from any string following. In particular, if the character immediately following the name is a letter, digit, or underscore, and the variable is not a positional parameter, then the braces are required:
You: a='This is a' echo "${a}n example" System: This is an example You: echo "$a test" System: This is a test
Refer to Conditional Substitution for a different use of braces in variable substitutions.
The shell uses the following variables. While the shell sets some of them, you can set or reset all of them:
CDPATH | Specifies the search path for the cd (change directory) command. |
HOME | Indicates the name of your login directory, the directory that becomes the current directory upon completion of a login. The login program initializes this variable. The cd command uses the value of the $HOME variable as its default value. Using this variable rather than an explicit path name in a shell procedure allows the procedure to be run from a different directory without alterations. |
IFS | The characters that are internal field separators (the characters that the shell uses during blank interpretation; see Blank Interpretation ). The shell initially sets the IFS variable to include the blank, tab, and new-line characters. |
LANG | Determines the locale to use for the locale categories when both the LC_ALL variable and the corresponding environment variable (beginning with LC_) do not specify a locale. For more information about locales, see "Locale Overview" in AIX Version 4.3 System Management Concepts: Operating System and Devices. |
LC_ALL | Determines the locale to be used to override any values for locale categories specified by the settings of the LANG environment variable or any environment variables beginning with LC_. For more information about locales and the LANG environment variable, see "Locale Overview" in AIX Version 4.3 System Management Concepts: Operating System and Devices. |
LC_COLLATE | Defines the collating sequence to use when sorting names and when character ranges occur in patterns. For information about locales and the LANG environment variable, see "Locale Overview" in AIX Version 4.3 System Management Concepts: Operating System and Devices. |
LC_CTYPE | Determines the locale for the interpretation of sequences of bytes of text data as characters (that is, single- versus multibyte characters in arguments and input files), which characters are defined as letters (alpha character class), and the behavior of character classes within pattern matching. For more information about locales, see "Locale Overview" in AIX Version 4.3 System Management Concepts: Operating System and Devices. |
LC_MESSAGES | Determines the language in which messages should be written. For information about locales and the LANG environment variable, see "Locale Overview" in AIX Version 4.3 System Management Concepts: Operating System and Devices. |
LIBPATH | Specifies the search path for shared libraries. |
LOGNAME | Specifies your login name, marked readonly in the /etc/profile file. |
Indicates the path name of the file used by the mail system to detect the arrival of new mail. If this variable is set, the shell periodically checks the modification time of this file and displays the value of $MAILMSG if the time changes and the length of the file is greater than 0. Set the MAIL variable in the .profile file. The value normally assigned to it by users of the mail command is /usr/spool/mail/$LOGNAME. | |
MAILCHECK | The number of seconds that the shell lets elapse before checking again for the arrival of mail in the files specified by the MAILPATH or MAIL variables. The default value is 600 seconds (10 minutes). If you set the MAILCHECK variable to 0, the shell checks before each prompt. |
MAILMSG | The mail notification message. If you explicitly set the MAILMSG variable to a null string (MAILMSG=""), no message is displayed. |
MAILPATH | A list of file names separated by colons. If this variable is set, the shell informs you of the arrival of mail in any of the files specified in the list. You can follow each file name by a % (percent sign) and a message to be displayed when mail arrives. Otherwise, the shell uses the value of the MAILMSG variable or, by default, the message [YOU HAVE NEW MAIL].
Note: When the MAILPATH variable is set, these files are checked instead of the file set by the MAIL variable. To check the files set by the MAILPATH variable and the file set by the MAIL variable, specify the MAIL file in your list of MAILPATH files. |
PATH | The search path for commands, which is an ordered list of directory path names separated by colons. The shell searches these directories in the specified order when it looks for commands. A null string anywhere in the list represents the current directory.
The PATH variable is normally initialized in the /etc/environment file, usually to /usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin. You can reset this variable to suit your own needs. The PATH variable provided in your .profile file also includes $HOME/bin and your current directory. If you have a project-specific directory of commands, for example, /project/bin, that you want searched before the standard system directories, set your PATH variable as follows: PATH=/project/bin:$PATH The best place to set your PATH variable to a value other than the default value is in your $HOME/.profile file. You cannot reset the PATH variable if you are executing commands under the restricted shell. |
PS1 | The string to be used as the primary system prompt. An interactive shell displays this prompt string when it expects input. The default value of the PS1 variable is $ (dollar sign) followed by a blank space, for nonroot users. |
PS2 | The value of the secondary prompt string. If the shell expects more input when it encounters a new-line character in its input, it prompts with the value of the PS2 variable. The default value of the PS2 variable is > (greater than sign), followed by a blank space. |
SHACCT | The name of a file that you own. If this variable is set, the shell writes an accounting record in the file for each shell script executed. You can use accounting programs such as acctcom and acctcms to analyze the data collected. |
SHELL | The path name of the shell, which is kept in the environment. This variable should be set and exported by the $HOME/.profile file of each restricted login. |
TIMEOUT | The number of minutes a shell remains inactive before it exits. If this variable is set to a value greater than 0 (zero), the shell exits if a command is not entered within the prescribed number of seconds after issuing the PS1 prompt. (Note that the shell can be compiled with a maximum boundary that cannot be exceeded for this value.) A value of zero indicates no time limit. |
Several variables have special meanings. The following variables are set only by the shell.
$@ | Expands the positional parameters, beginning with $1. Each parameter is separated by a space.
If you place " (double quotation marks) around $@, the shell considers each positional parameter a separate string. If no positional parameters exist, the Bourne shell expands the statement to an unquoted null string. |
$* | Expands the positional parameters, beginning with $1. The shell separates each parameter with the first character of the IFS variable value.
If you place " (double quotation marks) around $*, the shell includes the positional parameter values, in double quotes. Each value is separated by the first character of the IFS variable. |
$# | Specifies the number of positional parameters passed to the shell, not counting the name of the shell procedure itself. The $# variable thus yields the number of the highest-numbered positional parameter that is set. One of the primary uses of this variable is to check for the presence of the required number of arguments. Only positional parameters $0 through $9 are accessible through the shell. See "Positional Parameters" for more information. |
$? | Specifies the exit value of the last command executed. Its value is a decimal string. Most commands return a value of 0 to indicate successful completion. The shell itself returns the current value of the $? variable as its exit value. |
$$ | Identifies the process number of the current process. Because process numbers are unique among all existing processes, this string is often used to generate unique names for temporary files.
The following example illustrates the recommended practice of creating temporary files in a directory used only for that purpose: temp=/tmp/$$ ls >$temp . . . rm $temp |
$! | Specifies the process number of the last process run in the background using the & (ampersand) terminator. |
$- | A string consisting of the names of the execution flags currently set in the shell. |
After the shell performs variable and command substitution, it scans the results for internal field separators (those defined in the IFS shell variable). The shell splits the line into distinct words at each place it finds one or more of these characters separating each distinct word with a single space. It then retains explicit null arguments ("" or '') and discards implicit null arguments (those resulting from parameters that have no values).
Normally, the shell replaces the expression $Variable with the string value assigned to the Variable variable, if there is one. However, there is a special notation that allows conditional substitution, depending on whether the variable is set or not null, or both. By definition, a variable is set if it has ever been assigned a value. The value of a variable can be the null string, which you can assign to a variable in any one of the following ways:
Efg='' | Assigns the null string to the A, bcd, and Efg. |
set '' "" | Sets the first and second positional parameters to the null string and unsets all other positional parameters. |
The following is a list of the available expressions you can use to perform conditional substitution:
In conditional substitution, the shell does not evaluate the String variable until the shell uses this variable as a substituted string. Thus, in the following example, the shell executes the pwd command only if d is not set or is null:
echo ${d:-`pwd`}
When you run a shell procedure, the shell implicitly creates positional parameters that reference each word on the command line by its position on the command line. The word in position 0 (the procedure name) is called $0, the next word (the first parameter) is called $1, and so on, up to $9. To refer to command line parameters numbered higher than 9, use the built-in shift command.
You can reset the values of the positional parameters explicitly by using the built-in set command.
Note: When an argument for a position is not specified, its positional parameter is set to null. Positional parameters are global and can be passed to nested shell procedures.
Command parameters are often file names. You can automatically produce a list of file names as parameters on a command line. To do this, specify a character that the shell recognizes as a pattern-matching character. When a command includes such a character, the shell replaces it with the file names in a directory.
Note: The Bourne shell does not support file name expansion based on equivalence classification of characters.
Most characters in such a pattern match themselves, but you can also use some special pattern-matching characters in your pattern. These special characters are:
Within square brackets, a pair of characters separated by a - (hyphen) specifies the set of all characters lexically within the inclusive range of that pair, according to the binary ordering of character values.
Pattern matching has some restrictions. If the first character of a file name is a . (dot), it can be matched only by a pattern that also begins with a dot. For example, * (asterisk) matches the file names myfile and yourfile but not the file names .myfile and .yourfile. To match these files, use a pattern such as the following:
.*file
If a pattern does not match any file names, then the pattern itself is returned as the result of the attempted match.
File and directory names should not contain the characters * (asterisk), ? (question mark), [ (left bracket), or ] (right bracket) because they can cause infinite recursion (that is, infinite loops) during pattern-matching attempts.
You can also use character classes to match file names:
[[:charclass:]]
This format instructs the system to match any single character belonging to the specified class. The defined classes correspond to ctype subroutines. They are: