Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 458
rubber duck Avatar asked Jan 27 '23 02:01

rubber duck


1 Answers

${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

like image 85
jeremysprofile Avatar answered Jan 31 '23 04:01

jeremysprofile