Case insensitive comparison in Bash

前端 未结 5 1704
时光说笑
时光说笑 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条回答
  •  -上瘾入骨i
    2021-01-05 11:13

    I prefer using grep -E or egrep for short:

    while egrep -iq '^(y|yes)$' <<< $yn; do
      your_logic_here
    done
    

    Here you have explanations from grep manual:

    -i, --ignore-case

    Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)

    -q, --quiet, --silent

    Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.)

提交回复
热议问题