Blog Archive for April 25, 2019

Bash Uppercase Input with Defaults

April 25, 2019

I needed to modify a bash function that accepted user input, so that it uppercased that input. This was implemented using a function that provided a default value.

fetchInputUpper()
{
   local entered_value
   read -p "$1 [default: $2]: " entered_value
   export $3="${entered_value:-$2}"
   export $3="${!3^^}"
}

fetchInputUpper "Enter Text?" "Default" VARIABLE
echo …

Uppercase In Bash

April 25, 2019

If your bash version is greater than 4.0, you can use the ^^ syntax to uppercase variable values:

export LOWER="abc"
export UPPER=${LOWER^^}
echo $UPPER