How to compare strings in Bash

前端 未结 10 1764
眼角桃花
眼角桃花 2020-11-22 02:48

How do I compare a variable to a string (and do something if they match)?

10条回答
  •  遇见更好的自我
    2020-11-22 03:28

    I would probably use regexp matches if the input has only a few valid entries. E.g. only the "start" and "stop" are valid actions.

    if [[ "${ACTION,,}" =~ ^(start|stop)$ ]]; then
      echo "valid action"
    fi
    

    Note that I lowercase the variable $ACTION by using the double comma's. Also note that this won't work on too aged bash versions out there.

提交回复
热议问题