1

Following is a part from a shell script written by someone in my organization.

while read -r line
do
    #split if needed
    arrName=$(echo $line | cut -d"." -f1)
    if [ {$HOSTNAME^^} == {$arrName^^} ]; then
        echo "Perfect $arrName"
        echo "$1" >> $TEMPFILE
    fi
done < compuetrnames.txt

The entire script was understandable for me except this one line

if [ {$HOSTNAME^^} == {$arrName^^} ]; then

Can someone help me understand this line.

2

${variable^^} is a Bash 4.x parameter expansion which yields the variable's value converted to uppercase.

The dollar sign needs to go before the opening brace, not after.

The (improperly quoted) echo | cut pipeline could similarly be written with a parameter expansion ${line%%.*}; or you could use while IFS='.' read line _; do... to split the input on the first full stop immediately when you read it.

|improve this answer|||||
2

The ${parameter^^pattern} substitution in Bash will change the case of the characters in $parameter that matches the globbing pattern pattern to upper-case.

With no pattern, it changes the case of all characters in $parameter.

As tripleee pointed out in his answer, {$...} is a syntax error, or rather, {$HOSTNAME^^} will expand to the string {hostname^^} if your $HOSTNAME value is hostname.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.