Bash matching

From wikinotes
Revision as of 13:12, 29 August 2021 by Will (talk | contribs) (→‎regex... sortof)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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