String
Operators
  • =, equal
  • !=, not equal
  • -z, checks if the given string operand size is zero
  • -n, checks if the given string operand size is non-zero
  • str, if str is empty, return false
  • #!/bin/bash
    
    a="abc"
    b="def"
    
    if [ ! $a = $b ]
    then
    	echo "Not same ..."
    fi
    
    if [ -n $a ]
    then
    	echo "Size is none zero ..."
    fi
    			
    String manipulation
    #!/bin/bash
    
    a="abc"
    
    # length
    echo "Size: ${#a}"
    echo `expr length $a`
    
    # match substring at the beginning of string
    stringZ=abcABC123ABCabc
    echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
    
    # index of substring in string
    echo `expr index "$stringZ" C12` # 6
    
    # Substring Extraction
    # ${string:position}
    # ${string:position:length}
    echo ${stringZ:1} # bcABC123ABCabc
    echo ${stringZ:7:3} # 23A
    echo ${stringZ:(-4)} # Cabc
    # expr substr $string $position $length
    echo `expr substr $stringZ 1 2` # ab
    # expr match "$string" '\($substring\)'
    echo `expr match "$stringZ" '\(.[b-c]*[A-Z]..[0-9]\)'` # bcABC1
    echo `expr "$stringZ" : '\(.[b-c]*[A-Z]..[0-9]\)'`     # abcABC1
    
    # Substring Removal
    # ${string#substring}, deletes shortest match of $substring from front of $string
    echo ${stringZ#a*C} # 123ABCabc
    # ${string##substring}, deletes longest match of $substring from front of $string
    echo ${stringZ##a*C}     # abc
    # ${string%substring}, deletes shortest match of $substring from back of $string
    # ${string%%substring}, deletes longest match of $substring from back of $string
    
    # Substring Replacement
    # ${string/substring/replacement}, replace the first match
    echo ${stringZ/abc/xyz} # xyzABC123ABCabc
    # ${string//substring/replacement}, replace all matches
    echo ${stringZ//abc/xyz}      # xyzABC123ABCxyz
    # ${string/#substring/replacement}, If $substring matches front end of $string, substitute $replacement for $substring
    echo ${stringZ/#abc/XYZ}          # XYZABC123ABCabc
    # ${string/%substring/replacement}, If $substring matches back end of $string, substitute $replacement for $substring
    echo ${stringZ/%abc/XYZ}          # abcABC123ABCXYZ
    			
    Reference