Expression Meaning
----------------------------------------------------
${#str} Length of $str
${str:pos} Extract substring from $str at
$pos
${str:pos:len} Extract $len chars from $str at
$pos
${str/sub/rep} Replace first match of $sub with
$rep
${str//sub/rep} Replace all matches of $sub with
$rep
${str/#sub/rep} If $sub matches front end of $str,
substitute $rep for $sub
${str/%sub/rep} If $sub matches back end of
substitute $rep for $sub
(1) s1 = s2
(2) s1 != s2
(3) s1 < s2
(4) s1 > s2
(5) -n s1
(6) -z s1
(1) s1 matches s2
(2) s1 does not match s2
(3) __TO-DO__
(4) __TO-DO__
(5) s1 is not null (contains one or more characters)
(6) s1 is null
Comparing two strings.
#!/bin/bash S1='string' S2='String' if [ "$S1"="$S2" ]; then echo "S1('$S1') is not equal to S2('$S2')" fi if [ $S1=$S1 ]; then echo "S1('$S1') is equal to S1('$S1')" fi
$ in="The quick brown fox" $ out=`echo $in | tr [:lower:] [:upper:]` $ echo "$out"THE QUICK BROWN FOX
I quote here a note from a mail, sent buy Andreas Beck, refering to use if [ $1 = $2 ].
This is not quite a good idea, as if either $S1 or $S2 is empty, you will get a parse error. x$1=x$2 or "$1"="$2" is better.
$ var=$(( 20 + 5 ))
$ expr 1 + 3 # 4
$ expr 2 - 1 # 1
$ expr 10 / 3 # 3
$ expr 20 % 3 # 2 (remainder)
$ expr 10 \* 3 # 30 (multiply)
Num String Text -------------------------------------------- -eq = Equal to == Equal to -ne != Not equal to -lt \< Less than -le Less than or equal to -gt \> Greater than -ge Greater than or equal to -z is empty -n is not empty