1

Possible Duplicate:
converting string to lower case in bash shell scripting

For example:

 echo *****Language translator*****
 echo please choose the language
 for Chinese enter c
 for French enter f

In a simple way I want to be able to recognize both C and c for Chinese; and the same thing for f and F, recognized as French.

Is there a way to convert everything to lower case?

Here part of the code:

if [ $language == c ];
then
echo "Enter the word to translate:"
read word_to_translate

4
  • 1
    This question actually has a lot more examples that might fit your needs. stackoverflow.com/questions/2264428/… Commented Feb 21, 2012 at 2:51
  • 1
    There's probably a better way to do this overall. How are you detecting a selection? Commented Feb 21, 2012 at 2:52
  • Please provide me an example using my example. Thanks
    – cacosud
    Commented Feb 21, 2012 at 3:31
  • I'm adding more content since this does not seems to be clear.
    – cacosud
    Commented Feb 21, 2012 at 3:46

4 Answers 4

11

You can use tr to switch the chars to lowercase/uppercase:

echo $language | tr '[A-Z]' '[a-z]'
1
  • 1
    what if they mispelled chinese or french?
    – cacosud
    Commented Feb 21, 2012 at 3:57
4

You can use the following case-modifiers (from man bash):

${parameter^}      # Convert the first character in ${parameter} to uppercase
${parameter^^}     # Convert all characters in ${parameter} to uppercase
${parameter,}      # Convert the first character in ${parameter} to lowercase
${parameter,,}     # Convert all characters in ${parameter} to lowercase

So your code might look something like this:

# Read one character into $lang, with a nice prompt.
read -n 1 -p "Please enter c for Chinese, or f for French: " lang
if [ "${lang,,}" == "c" ]; then
  echo "Chinese"
elif [ "${lang,,}" == "f" ]; then
  echo "French"
else
  echo "I don't speak that language."
fi
3
  • sorry but what does -n 1 -p means?
    – cacosud
    Commented Feb 21, 2012 at 4:04
  • 1
    @cacosud: -n 1 means just read one character without waiting for the user to press return, and -p means use the following as a prompt (a rather cleaner way of doing it than echo). Commented Feb 21, 2012 at 5:00
  • Sorry for being unclear; thanks for the explanation, @GordonDavisson. I've added a comment to help.
    – Adam Liss
    Commented Feb 21, 2012 at 14:38
2

Modern versions of tr have support for POSIX character classes [:upper:] and [:lower:]

tr -s '[:upper:]'  '[:lower:]' < inputfile > outputfile

All of the character classes are here:

http://ss64.com/bash/tr.html
1
  • 2
    Be careful with that '-s' -- it means "squeeze repeated chars into one" -- so, if you do echo "FOOZBALL" | tr -s '[:upper:]' '[:lower:]' you'll end up with "fozbal". Which may not be what you wanted. (For some reason, this variation of the answer is all over the Internets.)
    – Tom Hundt
    Commented Dec 27, 2018 at 6:09
2

I'd probably use a case statement, though there are other ways to do it:

read language
case "$language" in
([cC]) echo 'Chinese';;
([fF]) echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac

You could make a canonical assignment in the case if you need the values outside the switch:

read language
case "$language" in
([cC]) lingua='zh_tw'; echo 'Chinese';;
([fF]) lingua='fr_fr'; echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac
6
  • I'd always seen a case written as, e.g., c|C) echo 'Chinese';; Learn something new on SO every day!
    – Adam Liss
    Commented Feb 21, 2012 at 3:23
  • 1
    @AdamLiss: that also works. The 'real' Bourne shell doesn't like the open parenthesis before the expression; the POSIX shell (and hence bash and ksh) allows, but does not require, the leading parenthesis, and I use it because it makes it easier to 'match parentheses' in vim. The square brackets are part of the shell globbing, of course. Commented Feb 21, 2012 at 3:45
  • How can I use this in a if statement? for example
    – cacosud
    Commented Feb 21, 2012 at 3:46
  • 1
    You can't - or don't. The case is analogous to a multi-way if statement in its own right. Commented Feb 21, 2012 at 3:56
  • Re-re-reading the man page, I see that bash has also evolved to support "falling through" from one case to the next, optionally requiring the next pattern to match before executing the corresponding commands.
    – Adam Liss
    Commented Feb 21, 2012 at 14:36

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