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?
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
[[ foo =~ "fo" ]] would still succeed, for example). The difference is that any metacharacters inside the regex will be treated literally.
[[ "0" =~ ^[0-9]+$ ]]