6.5. Variables This section describes the following: 6.5.1. Variable Substitution ksh93 provides structured variables, such as pos.x and pos.y. To create either one, pos must already exist, and braces must be used to retrieve their values. Names beginning with .sh are reserved for use by ksh. No spaces should be used in the following expressions. The colon (:) is optional; if it's included, var must be nonnull, as well as set. Variable expression | Description |
---|
var=value ... | Set each variable var to a value. | ${var} | Use value of var; braces are optional if var is separated from the following text. They are required for array variables, and in ksh93 if a variable name contains periods. | ${var:-value} | Use var if set; otherwise, use value. | ${var:=value} | Use var if set; otherwise, use value and assign value to var. | ${var:?value} | Use var if set; otherwise, print value and exit (if not interactive). If value isn't supplied, print the phrase "parameter null or not set." | ${var:+value} | Use value if var is set; otherwise, use nothing. | ${#var} | Use the length of var. | ${#*} | Use the number of positional parameters. | ${#@} | | ${var#pattern} | Use value of var after removing pattern from the left. Remove the shortest matching piece. | ${var##pattern} | Same as #pattern, but remove the longest matching piece. | ${var%pattern} | Use value of var after removing pattern from the right. Remove the shortest matching piece. | ${var%%pattern} | Same as %pattern, but remove the longest matching piece. |
In ksh93 and Bash: Variable expression | Description |
---|
${!prefix*} | List of variables whose names begin with prefix. | ${!prefix@} | | ${var:pos} | Starting at position pos (0-based) in variable var, extract len characters, or rest of string if no len. pos and len may be arithmetic expressions. | ${var:pos:len} | | ${var/pat/repl} | Use value of var, with first match of pat replaced with repl. | ${var/pat} | Use value of var, with first match of pat deleted. | ${var//pat/repl} | Use value of var, with every match of pat replaced with repl. | ${var/#pat/repl} | Use value of var, with match of pat replaced with repl. Match must occur at beginning of the value. | ${var/%pat/repl} | Use value of var, with match of pat replaced with repl. Match must occur at end of the value. |
In ksh93, indirect variables allow you to "alias" one variable name to affect the value of another. This is accomplished using typeset -n: $ greet="hello, world" Create initial variable $ typeset -n friendly_message=greet Set up alias $ echo $friendly_message Access old value through new name hello, world $ friendly_message="don't panic" Change the value $ echo $greet Old variable is changed don't panic Bash has a similar mechanism for indirect variable referencing: $ greet="hello, world" Create initial variable $ friendly_message=greet Aliasing variable $ echo ${!friendly_message} Use the alias hello, world 6.5.1.1 Examples $ u=up d=down blank= Assign values to three variables (last is null) $ echo ${u}root Braces are needed here uproot $ echo ${u-$d} Display value of u or d; since u is set, it's printed up $ echo ${tmp-`date`} If tmp is not set, the date command is executed Mon Aug 30 11:15:23 EDT 2004 $ echo ${blank="no data"} blank is set, so it is printed (a blank line) $ echo ${blank:="no data"} blank is set but null, so the string is printed no data $ echo $blank blank now has a new value no data $ tail=${PWD##*/} Take the current directory name and remove the longest character string ending with /, which removes the leading pathname and leaves the tail 6.5.2. Built-in Shell Variables Built-in variables are automatically set by the shell and are typically used inside shell scripts. Built-in variables can make use of the variable substitution patterns shown previously. Note that the $S is not actually part of the variable name, although the variable is always referenced this way. The following are available in any Bourne-compatible shell. Variable | Variable |
---|
$# | Number of command-line arguments. | $- | Options currently in effect (arguments supplied on command line or to set). | $? | Exit value of last executed command. | $$ | Process number of current process. | $! | Process number of last background command. | $0 | First word; that is, command name. This will have the full pathname if it was found via a PATH search. | $n | Individual arguments on command line (positional parameters). The Bourne shell allows only nine parameters to be referenced directly (n = 1-9); Bash and the Korn shell allow n to be greater than 9 if specified as ${n}. | $*, $@ | All arguments on command line ($1 $2 ...). | "$*" | All arguments on command line as one string ("$1 $2..."). The values are separated by the first character in IFS. | "$@" | All arguments on command line, individually quoted ("$1" "$2" ...). |
Bash and the Korn shell automatically set these additional variables. Variable | Variable |
---|
$_ | Temporary variable; initialized to pathname of script or program being executed. Later, stores the last argument of previous command. Also stores name of matching MAIL file during mail checks. | HISTCMD | The history number of the current command. | LINENO | Current line number within the script or function. | OLDPWD | Previous working directory (set by cd). | OPTARG | Name of last option processed by getopts. | OPTIND | Numerical index of OPTARG. | PPID | Process number of this shell's parent. | PWD | Current working directory (set by cd). | RANDOM[=n] | Generate a new random number with each reference; start with integer n, if given. | REPLY | Default reply, used by select and read. | SECONDS[=n] | Number of seconds since the shell was started, or, if n is given, number of seconds + n since the shell started. |
ksh93 automatically sets these additional variables. Variables whose names contain "." must be enclosed in braces when referencede.g., ${.sh.edchar}. Variable | Variable |
---|
.sh.edchar | The character(s) entered when processing a KEYBD trap. Changing it replaces the characters that caused the trap. | .sh.edcol | The position of the cursor in the most recent KEYBD trap. | .sh.edmode | Will be equal to Escape if in a KEYBD trap in vi mode; otherwise empty. | .sh.edtext | The characters in the input buffer during a KEYBD trap. | .sh.match | Array variable containing text matched during a variable substitution. Index 0 is the entire value; the others correspond to parenthesized subexpressions. | .sh.name | The name of the variable running a discipline function. | .sh.subscript | The subscript of the variable running a discipline function. | .sh.value | The value of the variable inside the set and get discipline functions. | .sh.version | The version of ksh93. |
Bash automatically sets these additional variables. Many of these variables are for use by the Bash Debugger (see http://bashdb.sourceforge.net) or for providing programmable completion (see the section Programmable Completion (Bash only)," later in this chapter). Variable | Variable |
---|
BASH | The full pathname used to invoke this instance of Bash. | BASH_ARGC | Array variable. Each element holds the number of arguments for the corresponding function or dot-script invocation. Set only in extended debug mode, with shopt -s extdebug. | BASH_ARGV | An array variable similar to BASH_ARGC. Each element is one of the arguments passed to a function or dot-script. It functions as a stack, with values being pushed on at each call. Thus, the last element is the last argument to the most recent function or script invocation. Set only in extended debug mode, with shopt -s extdebug. | BASH_COMMAND | The command currently executing or about to be executed. Inside a trap handler, it is the command running when the trap was invoked. | BASH_EXECUTION_STRING | The string argument passed to the -c option. | BASH_LINENO | Array variable, corresponding to BASH_SOURCE and FUNCNAME. For any given function number i (starting at 0), ${FUNCNAME[i]} was invoked in file ${BASH_SOURCE[i]} on line ${BASH_LINENO[i]}. The information is stored with the most recent function invocation first. | BASH_REMATCH | Array variable, assigned by the =~ operator of the [[ ]] construct. Index 0 is the text that matched the entire pattern. The other indices are the text matched by parenthesized subexpressions. This variable is read-only. | BASH_SOURCE | Array variable, containing source filenames. Each element corresponds with those in FUNCNAME and BASH_LINENO. | BASH_SUBSHELL | This variable is incremented by one each time a subshell or subshell environment is created. | BASH_VERSINFO[0] | The major version number, or release, of Bash. | BASH_VERSINFO[1] | The minor version number, or version, of Bash. | BASH_VERSINFO[2] | The patch level. | BASH_VERSINFO[3] | The build version. | BASH_VERSINFO[4] | The release status. | BASH_VERSINFO[5] | The machine type; same value as in MACHTYPE. | BASH_VERSION | A string describing the version of Bash. | COMP_CWORD | For programmable completion. Index into COMP_WORDS, indicating the current cursor position. | COMP_LINE | For programmable completion. The current command line. | COMP_POINT | For programmable completion. The position of the cursor as a character index in COMP_LINE. | COMP_WORDBREAKS | For programmable completion. The characters that the readline library treats as word separators when doing word completion. | COMP_WORDS | For programmable completion. Array variable containing the individual words on the command line. | DIRSTACK | Array variable, containing the contents of the directory stack as displayed by dirs. Changing existing elements modifies the stack, but only pushd and popd can add or remove elements from the stack. | EUID | Read-only variable with the numeric effective UID of the current user. | FUNCNAME | Array variable, containing function names. Each element corresponds with those in BASH_SOURCE and BASH_LINENO. | GROUPS | Array variable containing the list of numeric group IDs in which the current user is a member. | HISTCMD | The history number of the current command. | HOSTNAME | The name of the current host. | HOSTTYPE | A string that describes the host system. | MACHTYPE | A string that describes the host system in the GNU cpu-company-system format. | OSTYPE | A string that describes the operating system. | PIPESTATUS | An array variable containing the exit statuses of the commands in the most recent foreground pipeline. | SHELLOPTS | A colon-separated list of shell options (for set -o). If set in the environment at startup, Bash enables each option present in the list. | SHLVL | Incremented by one every time a new Bash starts up. | UID | Read-only variable with the numeric real UID of the current user. |
6.5.3. Other Shell Variables The following variables are not automatically set by the shell, although many of them can influence the shell's behavior. They are typically used in your .profile file, where you can define them to suit your needs. Variables can be assigned values by issuing commands of the form: variable=value This list includes the type of value expected when defining these variables. Those that are specific to the Bash shell are marked as (B). Those that are specific to the Korn shell are marked as (K). Those that are specific to ksh93 are marked (K93). Variable expression | Variable expression |
---|
CDPATH=dirs | Directories searched by cd; allows shortcuts in changing directories; unset by default. | COLUMNS=n | Screen's column width; used in line edit modes and select lists. | COMPREPLY=(words ...) | (B) Array variable from which Bash reads the possible completions generated by a completion function. | EDITOR=file | (K) Pathname of line-edit mode to turn on (can end in emacs or vi); used when VISUAL is not set. | EMACS | (B) If the value starts with t, Bash assumes it's running in an Emacs buffer and disables line editing. | ENV=file | Name of script that gets executed at startup; useful for storing alias and function definitions. For example, ENV=$HOME/.kshrc. | FCEDIT=file | Editor used by fc command (default is /bin/ed). Obsoleted in ksh93 by HISTEDIT. | FIGNORE=pattern | (K93) Pattern describing the set of filenames to ignore during pattern matching. (B) Similar: colon-separated list of patterns describing filenames to ignore when doing filename completion. | FPATH=dirs | (K) Directories to search for function definitions; undefined functions are set via typeset -fu; FPATH is searched when these functions are first referenced. (ksh93 also searches PATH.) | GLOBIGNORE=patlist | (B) Colon-separated list of patterns describing the set of filenames to ignore during pattern matching. | HISTCONTROL=list | (B) Colon-separated list of values controlling how commands are saved in the history file. Recognized values are: ignoredups, ignorespace, ignoreboth, and erasedups. | HISTEDIT=file | (K93) Editor used by hist command, if set. Overrides the setting of FCEDIT. | HISTFILE=file | File in which to store command history. For ksh, it must be set before ksh is started, and the default is $HOME/.sh_history. If you use both Bash and ksh, be sure to have different files for this value, as the format of the saved history file is not compatible between the two shells. | HISTFILESIZE=n | (B) Number of lines to be kept in the history file. This may be different than the number of commands. | HISTIGNORE=list | (B) A colon-separated list of patterns that must match the entire command line. Matching lines are not saved in the history file. An unescaped & in a pattern matches the previous history line. | HISTSIZE=n | Number of history commands to be kept in the history file. | HISTTIMEFORMAT=string | (B) A format string for strftime(3) to use for printing timestamps along with commands from the history command. If set (even if null), Bash saves timestamps in the history file along with the commands. | HOME=dir | Home directory; set by login (from /etc/passwd file). | HOSTFILE=file | (B) Name of a file in the same format as /etc/hosts that Bash should use to find hostnames for hostname completion. | IFS='chars' | Input field separators; default is space, tab, and newline. | IGNOREEOF=n | (B) Numeric value indicating how many successive EOF characters must be typed before Bash exits. If null or nonnumeric value, default is 10. | INPUTRC=file | (B) Initialization file for the readline library. This overrides the default value of ~/.inputrc. | LANG=dir | Default value for locale, used if no LC_* variables are set. | LC_ALL=locale | (B, K93) Current locale; overrides LANG and the other LC_* variables. | LC_COLLATE=locale | (B, K93) Locale to use for character collation (sorting order). | LC_CTYPE=locale | (B, K93) Locale to use for character class functions. (See the earlier section Filename Metacharacters.") | LC_MESSAGES=locale | (B) Locale to use for translating $"..." strings. | LC_NUMERIC=locale | (B, K93) Locale to use for the decimal-point character. | LINES=n | Screen's height; used for select lists. | MAIL=file | Default file to check for incoming mail; set by login. | MAILCHECK=n | Number of seconds between mail checks; default is 600 (10 minutes). | MAILPATH=files | One or more files, delimited by a colon, to check for incoming mail. Along with each file, you may supply an optional message that the shell prints when the file increases in size. Messages are separated from the filename by a ? character, and the default message is You have mail in $_. $_ is replaced with the name of the file. For example, you might have: MAILPATH="$MAIL?Ring! Candygram!:/etc/motd?New Login Message" | OPTERR=n | (B) When set to 1 (the default value), Bash prints error messages from the built-in getopts command. | PATH=dirlist | One or more pathnames, delimited by colons, in which to search for commands to execute. Default for many systems is /bin:/usr/bin. On Solaris, the default is /usr/bin:. However, the standard start-up scripts change it to: /usr/bin:/usr/ucb:/etc:. ksh93: PATH is also searched for function definitions for undefined functions. | POSIXLY_CORRECT=string | (B) When set at startup or while running, Bash enters POSIX mode, disabling behavior and modifying features that conflict with the POSIX standard. | PROMPT_COMMAND=command | (B) If set, Bash executes this command each time before printing the primary prompt. | PS1=string | Primary prompt string; default is $. | PS2=string | Secondary prompt (used in multiline commands); default is >. | PS3=string | Prompt string in select loops; default is #?. | PS4=string | Prompt string for execution trace (ksh -x, bash -x, or set -x); default is +. | SHELL=file | Name of default shell (e.g., /bin/sh). Bash sets this if it's not in the environment at startup. | TERM=string | Terminal type. | TIMEFORMAT=string | (B) A format string for the output for the time keyword. | TMOUT=n | If no command is typed after n seconds, exit the shell. Also affects the read command and the select loop. | VISUAL=path | (K) Same as EDITOR, but VISUAL is checked first. | auto_resume=list | (B) Enables the use of simple strings for resuming stopped jobs. With a value of exact, the string must match a command name exactly. With a value of substring, it can match a substring of the command name. | histchars=chars | (B) Two or three characters that control Bash's csh-style history expansion. The first character signals a history event. The second is the "quick substitution" character, and the third indicates the start of a comment. The default value is !^#. |
6.5.4. Arrays Both shells support one-dimensional arrays. The first element is numbered 0. Bash has no limit on the number of elements. ksh88 allowed up 1024 elements, early versions of ksh93 allowed at least 4096 elements, and modern versions allow up to 65,536 elements. Arrays are initialized with a special form of assignment: message=(hi there how are you today) Bash and ksh93 where the specified values become elements of the array. The Korn shell has an additional syntax: set -A message hi there how are you today Ksh88 and ksh93 Individual elements may also be assigned to: message[0]=hi This is the hard way message[1]=there message[2]=how message[3]=are message[4]=you message[5]=today Declaring arrays is not required. Any valid reference to a subscripted variable can create an array. When referencing arrays, use the ${ ... } syntax. This isn't needed when referencing arrays inside (( )) (the form of let that does automatic quoting). Note that [ and ] are typed literally (i.e., they don't stand for optional syntax). Syntax | Effect |
---|
${name[i]} | Use element i of array name. i can be any arithmetic expression as described under let. | ${name} | Use element 0 of array name. | ${name[*]} | Use all elements of array name. | ${name[@]} | | ${#name[*]} | Use the number of elements in array name. | ${#name[@]} | |
ksh93 provides associative arrays, where the indices are strings instead of numbers (as in awk). In this case, [ and ] act like double quotes. Associative arrays are created with typeset -A. A special syntax allows assigning to multiple elements at once: data=([joe]=30 [mary]=25) The values would be retrieved as ${data[joe]} and ${data[mary]}. 6.5.5. Discipline Functions (ksh93 only) Along with structured variables, ksh93 introduces discipline functions. These are special functions that are called whenever a variable's value is accessed or changed. For a shell variable named x, you can define the following functions:
x.get Called when x's value is retrieved ($x).
x.set Called when x's value is changed (x=2).
x.unset Called when x is unset (unset x). Within the discipline functions, special variables provide information about the variable being changed:
.sh.name The name of the variable being changed.
.sh.subscript The subscript of the array element being changed.
.sh.value The value of the variable being assigned or returned. Changing it within the discipline function changes the value that is actually assigned or returned. 6.5.6. Special Prompt Strings Both shells process the value of PS1 for special strings. The Korn shell expands a single ! into the current command number. Use !! to get a literal !. For example: PS1='cmd !> ' Bash processes the values of PS1, PS2, and PS4 for the following special escape sequences. Escape sequence | Description |
---|
\a | An ASCII BEL character (octal 07). | \A | The current time in 24-hour HH:MM format. | \d | The date in "weekday month day" format. | \D{format} | The date as specified by the strftime(3) format format. The braces are required. | \e | An ASCII Escape character (octal 033). | \h | The hostname, up to the first period. | \H | The full hostname. | \j | The current number of jobs. | \l | The basename of the shell's terminal device. | \n | A newline character. | \r | A carriage-return character. | \s | The name of the shell (basename of $0). | \t | The current time in 24-hour HH:MM:SS format. | \T | The current time in 12-hour HH:MM:SS format. | \u | The current user's username. | \v | The version of Bash. | \V | The release (version plus patch level) of Bash. | \w | The current directory, with $HOME abbreviated as ~. | \W | The basename of the current directory, with $HOME abbreviated as ~. | \! | The history number of this command. | \# | The command number of this command. | \$ | If the effective UID is 0, a #; otherwise a $. | \@ | The current time in 12-hour a.m./p.m. format. | \nnn | The character represented by octal value nnn. | \\ | A literal backslash. | \[ | Start a sequence of nonprinting characters, such as for highlighting or changing colors on a terminal. | \] | End a sequence of nonprinting characters. |
In addition, some or all of the PS1-PS4 variables undergo different substitutions, as outlined in the following table. Substitution | ksh88 | ksh93 | Bash |
---|
! for command number | PS1 | PS1 | | Escape sequences | | | PS1, PS2, PS4 | Variable substitution | PS1 | PS1 | PS1, PS2, PS4 | Command substitution | | PS1 | PS1, PS2, PS4 | Arithmetic substitution | | PS1 | PS1, PS2, PS4 |
In Bash, the escape sequences are processed first, and then, if the promptvars shell option is enabled via the shopt command (the default), the substitutions are performed. |