i am trying to grep for an integer inside some text using a bash script:
#!/bin/bash
var=string2
cat file.txt | grep -oP '(?<=string1)(\d.*)(<=$var)'
but the script outputs nothing.
if i put “string2” instead of “$var” i do get the expected result.
i guess i should escape the variable in some way, but i cant figure out how.
i tried double quotes, curly brackets, escaped double quotes, but nothing works.
i also searched stackoverflow man grep and google.
so what’s the proper way to do that?
thanks
Variables are only expanded inside double quotes, not single quotes. So it should be: