OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Case Conversion

Recent versions of bash have support for upper/lowercase conversion of strings. bash

has four parameter expansions and two options to the declare command to support it.

So what is case conversion good for? Aside from the obvious aesthetic value, it has an important role in programming. Let's consider the case of a database look-up. Imagine that a user has entered a string into a data input field that we want to look up in a data - base. It's possible the user will enter the value in all uppercase letters or lowercase letters or a combination of both. We certainly don't want to populate our database with every possible permutation of upper and lower case spellings. What to do?

A common approach to this problem is to normalize the user's input. That is, convert it


into a standardized form before we attempt the database look-up. We can do this by con- verting all of the characters in the user's input to either lower or uppercase and ensure that the database entries are normalized the same way.

The declare command can be used to normalize strings to either upper or lowercase. Using declare, we can force a variable to always contain the desired format no matter what is assigned to it:



#!/bin/bash

# ul-declare: demonstrate case conversion via declare declare -u upper

declare -l lower


if [[ $1 ]]; then

upper="$1" lower="$1" echo $upper echo $lower

fi

#!/bin/bash

# ul-declare: demonstrate case conversion via declare declare -u upper

declare -l lower


if [[ $1 ]]; then

upper="$1" lower="$1" echo $upper echo $lower

fi


In the above script, we use declare to create two variables, upper and lower. We assign the value of the first command line argument (positional parameter 1) to each of the variables and then display them on the screen:



[me@linuxbox ~]$ ul-declare aBc

ABC

abc

[me@linuxbox ~]$ ul-declare aBc

ABC

abc


As we can see, the command line argument ("aBc") has been normalized.

In addition to declare, there are four parameter expansions that perform upper/lower- case conversion:


Table 34-1: Case Conversion Parameter Expansions


Format Result

Format Result

${parameter,,} Expand the value of parameter into all lowercase.


image

${parameter,} Expand the value of parameter changing only the first

character to lowercase.


image

${parameter^^} Expand the value of parameter into all uppercase letters.


image


image

${parameter^} Expand the value of parameter changing only the first

character to uppercase (capitalization).


image


Here is a script that demonstrates these expansions:



#!/bin/bash

# ul-param: demonstrate case conversion via parameter expansion if [[ $1 ]]; then

echo ${1,,}

echo ${1,} echo ${1^^} echo ${1^}

fi

#!/bin/bash

# ul-param: demonstrate case conversion via parameter expansion if [[ $1 ]]; then

echo ${1,,}

echo ${1,} echo ${1^^} echo ${1^}

fi


Here is the script in action:



[me@linuxbox ~]$ ul-param aBc

abc aBc ABC

ABc

[me@linuxbox ~]$ ul-param aBc

abc aBc ABC

ABc


Again, we process the first command line argument and output the four variations sup- ported by the parameter expansions. While this script uses the first positional parameter, parameter may be any string, variable, or string expression.


Top OS Cloud Computing at OnWorks: