What is the correct way to escape the dollar sign in bash regex? I am trying to check if a string starts with a dollar sign. Here is my code in which I run away from the dollar twice in an expression of double quotes:
echo -e "AB1\nAB2\n\$EXTERNAL_REF\nAB3" | while read value; do if [[ ! $value =~ "^\\$" ]]; then echo $value else echo "Variable found: $value" fi done
This does what I want for a single box that has:
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
And the detailed output shows
+ [[ ! $EXTERNAL_REF =~ ^\$ ]] + echo 'Variable found: $EXTERNAL_REF'
However, in another field that uses
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
The comparison expands as follows
+ [[ ! $EXTERNAL_REF =~ \^\\\$ ]] + echo '$EXTERNAL_REF'
Is there a standard / better way to do this that will work in all implementations?
Thank you very much
source share