Search code examples
linuxbashscriptingcase-sensitive

How can I make a bash script case insensitive?


I'm writing a script that will output something about my friends depending on what name they input, but in the script I put the conditions for the if/then as their names capitalized. Is there a way I can make it so they can put in a lower case letter and it still be the same output?


Solution

  • In bash, you can use the ,, parameter expansion to lowercase a variable value:

    #! /bin/bash
    
    read -p Name: name
    case ${name,,} in
        (john) echo Hi John! ;;
        (jane) echo Hallo Jane! ;;
        (jack) echo Long time no see, Jack! ;;
        (*)    echo "I don't know you." ;;
    esac