Monday, October 26, 2009

List of Operators in Linux Shell Script

There are many operators available in linux shell script programming. In the following section it is discussed about the available operators in linux shell script.

A)Assignment Operator:
1)=: The assignment operator (=) is used to change the value of a variable or to assign value to a variable.
Example:
var=1
name=arju # Note that spaces are not allowed after or before the "=".

Note that "=" sign can be used as a test operator too.
Example:
if [ "$a" = "$b" ] #if value of variable a is equal to value of variable b.

B)Arithmetic operators:
Following is the list of arithmatic operators which is discussed in topic http://arjudba.blogspot.com/2009/04/arithmatic-operation-and-expression-in.html
1) + (plus)

2)- (minus)

3)* (multiplication)

4)/ (division)

5)** (exponentiation)

6)% (modulo, or mod (returns the remainder of an integer division operation))

7)+= (plus-equal (increment variable by a constant))

let "var += 7" results in var being incremented by 7.

8)-= (minus-equal (decrement variable by a constant))

9)*= (times-equal (multiply variable by a constant))

let "var *= 7" results in var being multiplied by 7.

10)/= (slash-equal (divide variable by a constant))

11)%= (mod-equal (remainder of dividing variable by a constant))

Note that arithmetic operators often occur in an expr or let expression.

C)bitwise operators
Bitwise operators too are discussed inside topic http://arjudba.blogspot.com/2009/04/arithmatic-operation-and-expression-in.html

1)<< (bitwise left shift (multiplies by 2 for each shift position)) 2)<<= (left-shift-equal) let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4) 3)>> (bitwise right shift (divides by 2 for each shift position))

4)>>= (right-shift-equal (inverse of <<=)) 5)& (bitwise AND) 6)&= (bitwise AND-equal) 7)| (bitwise OR) 8)|= (bitwise OR-equal) 9)~ (bitwise NOT) 10)^ (bitwise XOR) 11)^= (bitwise XOR-equal)

D)Logical (boolean) operators

1)! (NOT)

2)&& (AND)

3)|| (OR)

E)Miscellaneous Operators:

About comma operator it is discussed in http://arjudba.blogspot.com/2009/10/single-quote-double-quote-and-comma-in.html
1), (Comma operator).

Related Documents

No comments:

Post a Comment