If I do [[ "0" =~ "^[0-9]+$" ]] && echo hello
at a terminal I would expect to see the word "hello"
However, nothing gets printed. What am I doing wrong?
You need to remove the double quotes present in your regex. ie, don't enclose your regex pattern within double quotes.
[[ "0" =~ ^[0-9]+$ ]]
It should be:
[[ "0" =~ ^[0-9]+$ ]] && echo hello
Note that the second part is not surrounded with double quotes, otherwise it'll be treated as the string "^[0-9]+$" and not a regex. To confirm that, try:
[[ "^[0-9]+$" =~ "^[0-9]+$" ]] && echo hello
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With