Skip to content

bash String Manipulation

Different approaches to change string-content in bash.

String Length

${#string}

#EXAMPLE
stringZ=abcABC123ABCabc
echo ${#stringZ}                 # 15
echo `expr length $stringZ`      # 15
echo `expr "$stringZ" : '.*'`    # 15

Substring Extraction

${string:position}

Extracts substring from $string at $position. If the $string parameter is „*“ or „@“, then this extracts the positional parameters, [1] starting at $position.

${string:position:length}

Extracts $length characters of substring from $string at $position.

#EXAMPLE
stringZ=abcABC123ABCabc
#       0123456789.....
#       0-based indexing.
 
echo ${stringZ:0}                            # abcABC123ABCabc
echo ${stringZ:1}                            # bcABC123ABCabc
echo ${stringZ:7}                            # 23ABCabc
 
echo ${stringZ:7:3}                          # 23A
                                             # Three characters of substring.
 
# Is it possible to index from the right end of the string?
 
echo ${stringZ:-4}                           # abcABC123ABCabc
# Defaults to full string, as in ${parameter:-default}.
 
echo ${stringZ:(-4)}                         # Cabc
echo ${stringZ: -4}                          # Cabc

Substring Removal

${string#substring}

Deletes shortest match of $substring from front of $string.

${string##substring}

Deletes longest match of $substring from front of $string.

#EXAMPLE
    stringZ=abcABC123ABCabc
    #       |----|          shortest
    #       |----------|    longest
 
    echo ${stringZ#a*C}      # 123ABCabc
    # Strip out shortest match between 'a' and 'C'.
 
    echo ${stringZ##a*C}     # abc
    # Strip out longest match between 'a' and 'C'.

${string%substring}

Deletes shortest match of $substring from back of $string.


${string%%substring}

Deletes longest match of $substring from back of $string.

#EXAMPLE
    stringZ=abcABC123ABCabc
    #                    ||     shortest
    #        |------------|     longest
 
    echo ${stringZ%b*c}      # abcABC123ABCa
    # Strip out shortest match between 'b' and 'c', from back of $stringZ.
 
    echo ${stringZ%%b*c}     # a
    # Strip out longest match between 'b' and 'c', from back of $stringZ.

Substring Replacement

${string/substring/replacement}

Replace first match of $substring with $replacement. [2]

${string//substring/replacement}

Replace all matches of $substring with $replacement.

#EXAMPLE
    stringZ=abcABC123ABCabc
 
    echo ${stringZ/abc/xyz}       # xyzABC123ABCabc
                                  # Replaces first match of 'abc' with 'xyz'.
 
    echo ${stringZ//abc/xyz}      # xyzABC123ABCxyz
                                  # Replaces all matches of 'abc' with # 'xyz'.
 
    echo  ---------------
    echo "$stringZ"               # abcABC123ABCabc
    echo  ---------------
                                  # The string itself is not altered!
 
    # Can the match and replacement strings be parameterized?
    match=abc
    repl=000
    echo ${stringZ/$match/$repl}  # 000ABC123ABCabc
    #              ^      ^         ^^^
    echo ${stringZ//$match/$repl} # 000ABC123ABC000
    # Yes!          ^      ^        ^^^         ^^^
 
    echo
 
    # What happens if no $replacement string is supplied?
    echo ${stringZ/abc}           # ABC123ABCabc
    echo ${stringZ//abc}          # ABC123ABC
    # A simple deletion takes place.

${string/#substring/replacement}

If $substring matches front end of $string, substitute $replacement for $substring

${string/%substring/replacement}

If $substring matches back end of $string, substitute $replacement for $substring.

#EXAMPLE
stringZ=abcABC123ABCabc
echo ${stringZ/#abc/XYZ}          # XYZABC123ABCabc
                                      # Replaces front-end match of 'abc' with 'XYZ'.
echo ${stringZ/%abc/XYZ}          # abcABC123ABCXYZ
                                      # Replaces back-end match of 'abc' with 'XYZ'.