title: Bash - correct way to escape dollar in regex
body: <p>What is the correct way to escape a dollar sign in a bash regex? I am trying to test whether a string begins with a dollar sign. Here is my code, in which I double escape the dollar within my double quotes expression:</p>
  
  <pre><code>echo -e "AB1\nAB2\n\$EXTERNAL_REF\nAB3" | while read value;
  do
      if [[ ! $value =~ "^\\$" ]];
      then
              echo $value
      else
              echo "Variable found: $value"
      fi
  done
  </code></pre>
  
  <p>This does what I want for one box which has:</p>
  
  <pre><code>GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
  </code></pre>
  
  <p>And the verbose output shows</p>
  
  <pre><code>+ [[ ! $EXTERNAL_REF =~ ^\$ ]]
  + echo 'Variable found: $EXTERNAL_REF'
  </code></pre>
  
  <p>However, on another box which uses</p>
  
  <pre><code>GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
  </code></pre>
  
  <p>The comparison is expanded as follows</p>
  
  <pre><code>+ [[ ! $EXTERNAL_REF =~ \^\\\$ ]]
  + echo '$EXTERNAL_REF'
  </code></pre>
  
  <p>Is there a standard/better way to do this that will work across all implementations? </p>
  
  <p>Many thanks</p>