Thursday, July 9, 2015

String operations with wildcard in bash shell

To compare strings with wildcard in bash shell you can do as below (there's different between single and double brackets):

if [[ $a == z* ]] ...   # True if $a starts with an "z" (pattern matching).
if [[ $a == "z*" ]] ... # True if $a is equal to z* (literal matching).

if [ $a == z* ] ...     # File globbing and word splitting take place.
if [ "$a" == "z*" ] ... # True if $a is equal to z* (literal matching).


Reference: http://www.tldp.org/LDP/abs/html/comparison-ops.html