Bash matching: Difference between revisions
From wikinotes
Line 9: | Line 9: | ||
<source lang="bash"> | <source lang="bash"> | ||
[[ "file/path:100" =~ : ]] && echo "match" # if ':' is in string | |||
[[ "apple" =~ ap* ]] && echo "match" # ap(any-or-no characters) | [[ "apple" =~ ap* ]] && echo "match" # ap(any-or-no characters) | ||
[[ "apple" =~ ple ]] && echo "match" # if substring of 'apple' | [[ "apple" =~ ple ]] && echo "match" # if substring of 'apple' |
Latest revision as of 13:12, 29 August 2021
regex... sortof
Bash can be extended to have more fancy glob, but using purely the default shell, this is what we're looking at.
Technically, it is a subset of modern regex. be very careful to test your matches, this does not always behave you you'd expect it to.
[[ "file/path:100" =~ : ]] && echo "match" # if ':' is in string [[ "apple" =~ ap* ]] && echo "match" # ap(any-or-no characters) [[ "apple" =~ ple ]] && echo "match" # if substring of 'apple' [[ "apple" =~ "^apple$" ]] && echo "match" # ^/$ anchors to beginning/end of string [[ "apple" =~ "^[a-z]+$" ]] && echo "match" # regex, as you'd expect