Case insensitive comparison in Bash

前端 未结 5 1739
时光说笑
时光说笑 2021-01-05 10:37

I\'m trying to write a comparison in a while statement that\'s case insensitive. Basically, I\'m simply trying to shorten the following to act on a yes or no question promp

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 10:59

    No need to use shopt or regex. The easiest and quickest way to do this (as long as you have Bash 4):

    if [ "${var1,,}" = "${var2,,}" ]; then
      echo "matched"
    fi
    

    All you're doing there is converting both strings to lowercase and comparing the results.

提交回复
热议问题