rubber duck
rubber duck

Reputation: 83

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

Upvotes: 8

Views: 5620

Answers (2)

jeremysprofile
jeremysprofile

Reputation: 11425

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

Upvotes: 17

Walter A
Walter A

Reputation: 19982

The lines 2,3,4 are for constructing a variable name

(2)    _f="$(basename "${f}" .sh)"
(3)    _f="${_f#???-}"
(4)    __f="CT_DEBUG_${_f^^}"

In line 2 the path is removed and also the .sh at the end.
In line 3 the first 4 characters are removed when the fourth character is a -.
In line 4 it is appended to a string and converted to uppercase.
Let's see what happens to a/b/c.sh and ddd-eee.sh

       a/b/c.sh       ddd-eee.sh
(2)    c              ddd-eee
(3)    c              eee
(4)    CT_DEBUG_C     CT_DEBUG_EEE

Steps 2,3,4 can be replaced by 1 line:

__f=$(sed -r 's#(.*/)*(...-)?(.*).sh#CT_DEBUG_\U\3#' <<< "$f")

EDIT: First I had (...-)*, that would fail with aaa-bbb-c.sh: bbb- would be removed too!

In this case you don't have the variable _f what is used later in the code, so you might want 2 lines:

_f=$(sed -r 's#(.*/)*(...-)?(.*).sh#\3#' <<< "$f")
__f="CT_DEBUG_${_f^^}"

Upvotes: 1

Related Questions