regexbashparameter-expansion

Parameter expansion with double caret ^^


In the following block of code, how are line# 3 and 4 evaluated?

for f in "${CT_LIB_DIR}/scripts/build/debug/"*.sh; do
    _f="$(basename "${f}" .sh)"
    _f="${_f#???-}"
    __f="CT_DEBUG_${_f^^}"
done

Solution

  • ${PARAMETER#PATTERN}
    

    Substring removal

    This form is to remove the described pattern trying to match it from the beginning of the string. The operator "#" will try to remove the shortest text matching the pattern, while "##" tries to do it with the longest text matching.

    STRING="Hello world"
    echo "${STRING#??????}"
    >> world
    

    ${PARAMETER^}
    ${PARAMETER^^}
    ${PARAMETER,}
    ${PARAMETER,,}
    

    These expansion operators modify the case of the letters in the expanded text.

    The ^ operator modifies the first character to uppercase, the , operator to lowercase. When using the double-form (^^ and ,,), all characters are converted.

    Example:

    var="somewords"
    echo ${var^^}
    >> SOMEWORDS
    

    See more information on bash parameter expansion