Linux /bin/sh check if string contains X

前端 未结 3 977
终归单人心
终归单人心 2021-01-05 01:36

In a shell script, how can I find out if a string is contained within another string. In bash, I would just use =~, but I am not sure how I can do the same in /bin/sh. Is i

3条回答
  •  被撕碎了的回忆
    2021-01-05 01:43

    You can define a function

    matches() {
        input="$1"
        pattern="$2"
        echo "$input" | grep -q "$pattern"
    }
    

    to get regular expression matching. Note: usage is

    if matches input pattern; then
    

    (without the [ ]).

提交回复
热议问题