3 ms·
I've learned to only use [[ when I want to do a regex match, e.g. if [[ "${foo}" =~ ^bar$ ]]; then echo Yes; fi Otherwise, just stick with "test" or "[". 8
by metadat 3y ago
I've learned to only use [[ when I want to do a regex match, e.g.
if [[ "${foo}" =~ ^bar$ ]]; then echo Yes; fi
Otherwise, just stick with "test" or "[".
85,000 lines of bash and counting... Not saying bash is great, but it's still meeting my needs for a lot of stuff. Shrug.
- cvccvroomvroom 3y agoThat's a pointless example. [ "$foo" = bar ] && echo Yes For substring matches, [ and * globs are generally good enough. [ "$bar" = extra* ] && echo '$bar began with extra' Bash's regex dialect is primitive and rarely worth fussing over. For anything complicated, use another tool be it grep, awk, perl, or such. There are diminishing returns of obsessing over doing everything in bash when a complicated task demands more capabilities suitable to another tool with greater reusability, modularity, and intrinsic types.
- aidenn0 3y agoI thought regex inside [[ was the same as egrep?
- stephenr 3y agoIf you want to do (relatively) simple pattern matching in a posix-compliant way, `expr`[1] can match BREs, and even return a capture group. 1: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/e...
- somat 3y agoAs someone who enjoys shell scripting I don't think I have ever used the "expr" command. this astonishes and baffles me because I used to read the man pages for fun, I even had a little script to pick a random page. So thanks a bunch for bringing it to my attention. My only guess as to why I am unfamiliar with the command is that perhaps younger me failed to figure out "expression" means regular expression. It only clicked this time because of your comment and the see also: re_format link at the bottom. http://man.openbsd.org/expr http://man.openbsd.org/expr
- stephenr 3y agoYeah it's slightly obscure for a lot of people I think: most who do know of it seem to associate it purely with arithmetic, and rightly use `$((...))` instead.
- penguin_booze 3y agoThe fact that the regex is written bare and unquoted, got me a while ago. I'm someone who religiously quotes everything; it took me a while before I figured out why my stupidly simple regex doesn't match.