📄 a
字号:
Parameter Substitution A parameter is an identifier, one or more digits, or any of the characters *, @, #, ?, -, $, and !. A named parameter (a parameter denoted by an identifier) has a value and zero or more attributes. Named parameters can be assigned values and attributes by using the typeset special command. Attributes supported by ksh are described later with the typeset special command. Exported parameters pass values and attributes to the environment. The shell supports a limited one-dimensional array facility. An element of an array parameter is referenced by a subscript. A subscript is denoted by a [ followed by an arithmetic expression (see Arithmetic Evaluation below) followed by a ]. To assign values to an array, use set -A name value .... The value of all subscripts must be in the range of 0 through 1023. Arrays need not be declared. Any reference to a named parameter with a valid subscript is legal and an array is created if necessary. Referencing an array without a subscript is equivalent to referencing the first element. The value of a named parameter can also be assigned by writing: name=value [name=value] ... If the -i integer attribute is set for name, the value is subject to arithmetic evaluation as described below. Positional parameters, parameters denoted by a number, can be assigned values with the set special command. Parameter $0 is set from argument zero when the shell is invoked. The character $ is used to introduce substitutable parameters. ${parameter} Substitute the value of the parameter, if any. Braces are required when parameter is followed by a letter, digit, or underscore that should not be interpreted as part of its name or when a named parameter is subscripted. If parameter is one or more digits, it is a positional parameter. A positional parameter of more than one digit must be enclosed in braces. If parameter is * or @ all the positional parameters, starting with $1, are substituted (separated by a field separator character). If an array identifier with subscript * or @ is used, the value for each element is substituted (separated by a field separator character). The shell reads all the characters from ${ to the matching } as part of the same word even if it contains braces or metacharacters. Hewlett-Packard Company - 6 - HP-UX 11i Version 1: Sep 2002 ksh(1) ksh(1) ${#parameter} If parameter is * or @, the number of positional parameters is substituted. Otherwise, the length of the value of the parameter is substituted. ${#identifier[*]} Substitute the number of elements in the array identifier. ${parameter:-word} If parameter is set and is non-null, substitute its value; otherwise substitute word. ${parameter:=word} If parameter is not set or is null, set it to word; then substitute the value of the parameter. Positional parameters cannot be assigned in this way. ${parameter:?word} If parameter is set and is non-null, substitute its value; otherwise, print word and exit from the shell. If word is omitted, a standard message is printed. ${parameter:+word} If parameter is set and is non-null, substitute word; otherwise substitute nothing. ${parameter#pattern} ${parameter##pattern} If the shell pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted; otherwise the value of this parameter substituted. In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted. ${parameter%pattern} ${parameter%%pattern} If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise substitute the value of parameter. In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted. In the above, word is not evaluated unless it is used as the substituted string. Thus, in the following example, pwd is executed only if d is not set or is null: Hewlett-Packard Company - 7 - HP-UX 11i Version 1: Sep 2002 ksh(1) ksh(1) echo ${d:-$(pwd)} If the colon (:) is omitted from the above expressions, the shell only checks to determine whether or not parameter is set. The following parameters are set automatically by the shell: # The number of positional parameters in decimal. - Options supplied to the shell on invocation or by the set command. ? The decimal value returned by the last executed command. $ The process number of this shell. _ Initially, the value of _ is an absolute pathname of the shell or script being executed as passed in the environment. Subsequently it is assigned the last argument of the previous command. This parameter is not set for commands which are asynchronous. This parameter is also used to hold the name of the matching MAIL file when checking for mail. ! The process number of the last background command invoked. COLUMNS If this variable is set, its value is used to define the width of the edit window for the shell edit modes and for printing select lists. In a windowed environment, if the shell detects that the window size has changed, the shell updates the value of COLUMNS. ERRNO The value of errno as set by the most recently failed system call. This value is system dependent and is intended for debugging purposes. LINENO The line number of the current line within the script or function being executed. LINES If this variable is set, the value is used to determine the column length for printing select lists. select lists print vertically until about two-thirds of LINES lines are filled. In a windowed environment, if the shell detects that the window size has changed, the shell updates the value of LINES. OLDPWD The previous working directory set by the cd command. OPTARG The value of the last option argument processed by the getopts special command. OPTIND The index of the last option argument processed by the getopts special command. PPID The process number of the parent of the shell. PWD The present working directory set by the cd command. Hewlett-Packard Company - 8 - HP-UX 11i Version 1: Sep 2002 ksh(1) ksh(1) RANDOM Each time this parameter is evaluated, a random integer, uniformly distributed between 0 and 32767, is generated. The sequence of random numbers can be initialized by assigning a numeric value to RANDOM. REPLY This parameter is set by the select statement and by the read special command when no arguments are supplied. SECONDS Each time this parameter is referenced, the number of seconds since shell invocation is returned. If this parameter is assigned a value, the value returned upon reference is the value that was assigned plus the number of seconds since the assignment. The following parameters are used by the shell: CDPATH The search path for the cd command. EDITOR If the value of this variable ends in emacs, gmacs, or vi and the VISUAL variable is not set, the corresponding option is turned on (see set in Special Commands below). ENV If this parameter is set, parameter substitution is performed on the value to generate the path name of the script to be executed when the shell is invoked (see Invoking ksh below). This file is typically used for alias and function definitions. FCEDIT The default editor name for the fc command. FPATH The search path for function definitions. This path is searched when a function with the -u attribute is referenced and when a command is not found. If an executable file is found, then it is read and executed in the current environment. IFS Internal field separators, normally space, tab, and new-line that are used to separate command words resulting from command or parameter substitution, and for separating words with the special command read. The first character of the IFS parameter is used to separate arguments for the "$*" substitution (see Quoting below). HISTFILE If this parameter is set when the shell is invoked, its value is the path name of the file that is used to store the command history. The default value is $HOME/.sh_history. If the user has appropriate privileges and no HISTFILE is given, then no history file is used (see Command Re-entry below). HISTSIZE If this parameter is set when the shell is invoked, the number of previously entered commands accessible to this shell will be greater than or equal to this number. The default is 128. Hewlett-Packard Company - 9 - HP-UX 11i Version 1: Sep 2002 ksh(1) ksh(1) HOME The default argument (home directory) for the cd command. MAIL If this parameter is set to the name of a mail file and the MAILPATH parameter is not set, the shell informs the user of arrival of mail in the specified file. MAILCHECK This variable specifies how often (in seconds) the shell checks for changes in the modification time of any of the files specified by the MAILPATH or MAIL parameters. The default value is 600 seconds. When the time has elapsed the shell checks before issuing the next prompt. MAILPATH A list of file names separated by colons (:). If this parameter is set, the shell informs the user of any modifications to the specified files that have occurred within the last MAILCHECK seconds. Each file name can be followed by a ? and a message to be printed, in which case the message undergoes parameter and command substitution with the parameter $_ defined as the name of the changed file. The default message is you have mail in $_. PATH The search path for commands (see Execution below). The user cannot change PATH if executing rksh (except in the .profile file). PS1 The value of this parameter is expanded for parameter substitution, to define the primary prompt string which, by default, is $ followed by a space character. The character ! in the primary prompt string is replaced by the command number (see Command Re-entry below). To include a ! in the prompt, use !!. PS2 Secondary prompt string, by default > followed by a space character. PS3 Selection prompt string used within a select loop, by default #? followed by a space character. PS4 The value of this variable is expanded for parameter substitution and precedes each line of an execution trace. If PS4 is unset, the execution trace prompt is + followed by a space character. SHELL The path name of the shell is kept in the environment. When invoked, the shell is restricted if the value of this variable contains an r in the basename. TMOUT If set to a value greater than zero, the shell terminates if a command is not entered within the prescribed number of seconds after issuing the PS1 prompt. VISUAL Invokes the corresponding option when the value of this variable ends in emacs, gmacs, or vi (see set Hewlett-Packard Company - 10 - HP-UX 11i Version 1: Sep 2002 ksh(1) ksh(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -