I've really hard time to understand this behaviour:
stackExchange@test:~$ if [[ "two words" =~ \bwords ]]; then echo hi; fi; #(I'd expect this one worked)
stackExchange@test:~$ if [[ "two words" =~ \\bwords ]]; then echo hi; fi; #(or at least this one...)
stackExchange@test:~$ if [[ "two words" =~ \\\bwords ]]; then echo hi; fi;
stackExchange@test:~$ if [[ "two words" =~ \\\\bwords ]]; then echo hi; fi;
stackExchange@test:~$ put_in_a_variable=\\bwords
stackExchange@test:~$ if [[ "two words" =~ $put_in_a_variable ]]; then echo hi; fi;
hi
stackExchange@test:~$
I understand that my variable contains \bword
and this got expanded in the pattern section of the conditional expression, but I really cannot understand why seems impossible to achieve the same behaviour using inline shell escaping.
I don't want to do something like if [[ "two words" =~ $(echo \\bwords) ]]; then echo hi; fi;
; too weird...
Thanks,
Francesco
\b
regex symbol inline... just by "design" :\ – Taz Oct 29 '20 at 23:40\b
inline. You can do various tricks (eg.bs='\'; [[ k =~ ${bs}bk ]] && echo yeah
) -- but I find them even worse than thatrematch
wrapper function ;-) – Oct 30 '20 at 00:00shopt -s compat31; [[ k =~ '\bk' ]]
or switch to zsh which doesn't have that misfeature, and hasset -o rematchpcre
where\b
is guaranteed to be available (contrary to in default EREs) – Stéphane Chazelas Oct 07 '23 at 09:00