Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [71]

By Root 975 0
overridden by the other LC_* variables.

LC_ALL

Name of current locale; overrides LANG and the other LC_* variables.

LC_COLLATE

Name of current locale for character collation (sorting) purposes.

LC_CTYPE

Name of current locale for character class determination during pattern matching.

LC_MESSAGES

Name of current language for output messages.

LINENO

Line number in script or function of the line that just ran.

NLSPATH

The location of message catalogs for messages in the language given by $LC_MESSAGES (XSI).

PATH

Search path for commands.

PPID

Process ID of parent process.

PS1

Primary command prompt string. Default is "$ ".

PS2

Prompt string for line continuations. Default is "> ".

PS4

Prompt string for execution tracing with set -x. Default is "+ ".

PWD

Current working directory.

The special variable $$ is useful in scripting for creating unique (usually temporary) filenames based on the shell's process ID number. However, systems that have the mktemp command should use that instead. Both of these are discussed in Chapter 10.

Arithmetic Expansion

The shell arithmetic operators are equivalent to their counterparts in the C language. Precedence and associativity are the same as in C. Table 6-4 shows the arithmetic operators that are supported, in order from highest precedence to lowest. Although some of these are (or contain) special characters, there is no need to backslash-escape them, because they are within the $((...)) syntax. This syntax acts like double quotes, except that an embedded double quote need not be escaped (see Section 7.7).

Table 6-4. Arithmetic operators

Operator

Meaning

Associativity

++ --

Increment and decrement, prefix and postfix

Left to right

+ - ! ~

Unary plus and minus; logical and bitwise negation

Right to left

* / %

Multiplication, division, and remainder

Left to right

+ -

Addition and subtraction

Left to right

<< >>

Bit-shift left and right

Left to right

< <= > >=

Comparisons

Left to right

= = !=

Equal and not equal

Left to right

&

Bitwise AND

Left to right

^

Bitwise Exclusive OR

Left to right

|

Bitwise OR

Left to right

&&

Logical AND (short-circuit)

Left to right

||

Logical OR (short-circuit)

Left to right

?:

Conditional expression

Right to left

= += -= *= /= %= &= ^= <<= >>= |=

Assign ment opera tor s

Right to left

Parentheses can be used to group subexpressions. As in C, the relational operators (<, <=, >, >=, = =, and !=) produce a numeric result that acts as a truth value: 1 for true and 0 for false.

For example, $((3 > 2)) has the value 1; $(( (3 > 2) || (4 <= 1) )) also has the value 1, since at least one of the two subexpressions is true.

For the logical AND and OR operators, any nonzero value functions as true:

$ echo $((3 && 4))

Both 3 and 4 are "true"

1

This use of nonzero as "true" applies in just about all languages derived from C, such as C++, Java, and awk.

If you're familiar with C, C++, or Java, the operators listed in Table 6-4 will be familiar. If you're not, some of them warrant a little explanation.

The assignment forms of the regular operators are a convenient shorthand for the more conventional way of updating a variable. For example, in many languages you might write x = x + 2 to add 2 to x. The += operator lets you do that more compactly: $((x += 2)) adds 2 to x and stores the result back in x.

Since adding and subtracting one are such frequent operations, the ++ and -- operators provide an even more abbreviated way to do them. As you might guess, ++ adds one, and -- subtracts one. These are unary operators. Let's take a quick look at how they work:

$ i=5

$ echo $((i++)) $i

5 6

$ echo $((++i)) $i

7 7

What's going on here? In both cases, the value of i is increased by one. However, the value returned by the operator depends upon its placement relative to the variable being operated

Return Main Page Previous Page Next Page

®Online Book Reader