# [[ ]]: the Korn shell conditional command. x=hello e= sp='a b c' [[ $x == h* ]] && echo "glob match" [[ $x == "h*" ]] || echo "quoted pattern is literal" [[ $x = hel?o ]] && echo "single = also matches" [[ $x != w* ]] && echo "!= pattern" p='h*'; [[ $x == $p ]] && echo "pattern from variable" [[ $x == "$p" ]] || echo "quoted variable is literal" [[ $x == [gh]ello ]] && echo "bracket pattern" [[ $sp == "a b c" ]] && echo "no field splitting" [[ $e ]] || echo "empty string is false" [[ -n $x && -z $e ]] && echo "&&" [[ -z $x || -n $x ]] && echo "||" [[ ! -z $x ]] && echo "!" [[ ! ! -n $x ]] && echo "!!" [[ -n $x && ( -z $x || $x == hello ) ]] && echo "parentheses" [[ -z $x && -n $x || -n $x ]] && echo "&& binds tighter than ||" [[ a < b && b > a ]] && echo "< and > compare strings" [[ 10 < 9 ]] && echo "string order, not numeric" [[ 10 -lt 9 ]] || echo "-lt is numeric" [[ 1+2 -eq 3 ]] && echo "operands of -eq are arithmetic" n=4; [[ n*2 -ge 8 && $n -ne 5 && n -le 4 && n -gt 3 ]] && echo "-ge -ne -le -gt" [[ abc =~ ^a.c$ ]] && echo "regex" [[ abc =~ b ]] && echo "regex is unanchored" [[ abc =~ "a.c" ]] || echo "quoted regex text is literal" [[ a.c =~ "a.c" ]] && echo "... and matches itself" re='^(ab|cd)+$'; [[ abcdab =~ $re ]] && echo "regex from variable" [[ x =~ '(' ]] || echo "quoted ( is literal" [[ x =~ [ ]] 2>/dev/null; echo "bad regex status=$?" : >f; mkdir d; ln -s f lnk [[ -f f && -d d && -h lnk && -L lnk && -e f && -a f ]] && echo "file tests" [[ -f nope || -e nope ]] || echo "missing file" [[ f -ef lnk ]] && echo "-ef" set -o noglob; [[ -o noglob ]] && echo "-o option on"; set +o noglob [[ -o noglob ]] || echo "-o option off" [[ -o nosuchoption ]] || echo "-o unknown option is false" [[ -v x ]] && echo "-v set"; [[ -v e ]] && echo "-v set to empty" [[ -v nosuch ]] || echo "-v unset" [[ -f ]] && echo "lone -f is a string" [[ -f == -f ]] && echo "-f as an operand" [[ * == \* ]] && echo "no globbing of operands" [[ ~ == "$HOME" ]] && echo "tilde expansion" [[ $(echo hi) == hi ]] && echo "command substitution" [[ -z $e && $((1 + 1)) == 2 ]] && echo "arithmetic expansion" c=0; [[ -n "" && $((c += 1)) ]]; echo "&& short-circuits expansions: c=$c" [[ -n x || $((c += 1)) ]]; echo "|| short-circuits expansions: c=$c" [[ a == a ]]; echo "true=$?" [[ a == b ]]; echo "false=$?" [[ 1 -eq 1 ]] && [[ 2 -eq 2 ]] && echo "chained with &&" if [[ $x == hello ]]; then echo "in if"; fi while [[ $n -gt 2 ]]; do n=$((n - 1)); done; echo "while: n=$n" [[ $x == hello ]] >out && echo "redirection after ]]" [[ a /dev/null; echo "syntax $s: $?" done echo [[ and ]] are words elsewhere