03. Variables and Expressions (Part 2)

Expressions

Arithmetic Expressionse

+ add

subtract

* multiply

/ divide (remember that it’s integer division, so you’ll get an integer as a result. For example: 3 / 2 => 1 as a result)

% modulus (calculates the remainder of a division. For example:  5 / 3 => 1    5 % 3 => 2)

++ Pre-increment (value in variable is incremented by 1 first, then use in any other evaluation)

Example:  ++n * 3 means that n is incremented first, then the new n value is multiplied by 3

++ Post-increment (value in variable used in any necessary evaluation, then the value in the variable is incremented last)

Example:  n++ * 3 means that the original n value is multiplied by 3 first, then n is incremented

– – [2 minus sign next to each other] Pre-decrement (value in variable is decremented by 1 first, then use in any other evaluation)

Example:  --n * 3 means that n is decremented first, then the new n value is multiplied by 3

  – – [2 minus sign next to each other] Post-decrement (value in variable n is used in any necessary evaluation first, then n is decremented by 1)

Example:  n-- * 3 means that the original n value is multiplied by 3 first, then n is decremented

= assignment (store data value on the right side into variable on the left side)

*= multiply and assign (example: n *= 3 means n = n * 3)

/= divide and assign (example: n /= 3 means n = n / 3)

%= modulus and assign (example: n %= 3 means n = n % 3)

+= add and assign (example: n += 3 means n = n + 3)

-= subtract and assign (example: n -= 3 means n = n - 3)

Arithmetic Evaluation with let

Arithmetic Evaluation with ((  ))

Conditional Expressions

Relational Expression for Integers

< less than

> greater than

<= less than or equal to

>= greater than or equal to

== equal to

!= not equal to

-lt less than

-gt greater than

-le less than or equal to

-ge greater than or equal to

-eq equal to

-ne not equal to

-lt less than

-gt greater than

-le less than or equal to

-ge greater than or equal to

-eq equal to

-ne not equal to

Relational Expression for Strings

=   or  == equal to (matching)

!= not equal to (not matching)

\< less than (comes before, based on ascii order)

\> greater than (comes after, based on ascii order)

-z zero length or empty string

All string compares are ASCII compare of character by character

=   or  == equal to (matching)

!= not equal to (not matching)

< less than (comes before, based on ascii order)

> greater than (comes after, based on ascii order)

-z zero length or empty string

=~ matching a string with regular expression

Logical Expressions

! logical not:  reverses true to false, and false to true

Example:  if n is 2, then  [[ ! $n –gt  0 ]]  is false

       and  [ ! $n –gt  0 ]   is false

     and  (( ! n > 0 ))      is false

&&   and   -a

logical and:   true only if both left and right sides are true, otherwise false if at least one side is false

T&&T => T T&&F => F F&&T => F F&&F => F  

T –a T => T T –a F => F F –a T => F F –a F => F

&& is used with ((  ))  and  [[  ]]

-a is used with [  ] 

Example:  if n is 2, m is 4, then (( m > n && n < 0 ))   is false

and   [[ $m –gt  $n  &&  $n  -lt  0 ]]   is false

and    [ $m –gt  $n  -a  $n  -lt  0 ]      is false

||   and   -o

logical or:   false only if both left and right sides are false, otherwise true if at least one side is true

T || T => T T || F => T F || T => T F || F => F  

T –o T => T T –o F => T F –o T => T F –o F => F

|| is used with ((  ))  and  [[  ]]

-o is used with [  ] 

Example:  if n is 2, m is 4, then (( m > n || n < 0 ))     is true

and   [[ $m –gt  $n  ||  $n  -lt  0 ]]     is true

and    [ $m –gt  $n  -o  $n  -lt  0 ]     is true


File Test Expressions

-e exists

-f is a regular file

-d is a directory

-h is a symbolic link

-s has a non-zero size (not empty)

-r has read permission

-w has write permission

-x has execute permission

-nt is newer than

-ot is older than

-ef is hard linked with

If fA is a symbolic link and is older than fB

[ -e fA ]    evaluates to T

[[ -f  fA ]]    F

[ -d fA ]    F

[[ -h fA ]]    T

[ -s fA ]  T

[[ -r  fA ]]  T

[ -w fA ]  T

[[ -x  fA ]]  T

[ fA –nt  fB ]  F

[[ fA –ot  fB ]]  T

[[ fA –ef  fB ]]   F