Using match to find substrings in strings with only bash

后端 未结 4 2094
无人共我
无人共我 2020-12-14 00:42

Although I am almost sure this has been covered, I can\'t seem to find anything specific to this. As I continue my journey on learning bash I keep finding parts where I am b

4条回答
  •  既然无缘
    2020-12-14 00:52

    I made this simple function:

    match() {
        TRUE=1
        FALSE=0
        match_return=0
        echo $1 | grep $2 >/dev/null
        [ $? -eq 0 ] && match_return=$TRUE || match_return=$FALSE
    }
    

    Usage:

    match Testing Test ; [ $match_return -eq 1 ] && echo "match!" || echo "nope"
    

    entire code: https://gist.github.com/TeeBSD/5121b3711fad40a09455

提交回复
热议问题