Search code examples
regexbashseddereference

How does bash expand escaped characters when dereferencing variables


If I quit using variables, and just write the regexes directly into to the last sed command, everything works. But as it is here, no substitutions are done?

#!/bin/bash
#html substitutions
ampP="\&"
ampR="&"

ltP="\<"
ltR="<"

gtP="\&gt;"
gtR=">"

quotP="\&quot;"
quotP2='\&#8220;'
quotP3="\&#8221;"
quotR="\""

tripDotP="\&#8230"
tripDotR="..."

tickP="\&#8217;"
tickR="\´"

#get a random page, and filter out the quotes
#pick a random quote
#translate wierd html symbols
curl "www.yodaquotes.net/page/$((RANDOM % 9 +1))/" -Ls | sed -nr 's/.*data-text=\"([^\"]+)\".*/\1/p' \
| sort -R | head -n1 \
| sed 's/"$ampP"/"$ampR"/g; s/$ltP/$ltR/g; s/$gtP/$gtR/g; s/$quotP/$quotR/g; s/"$quotP2"/"$quotR"/g; s/$quotP3/$quotR/g; s/$tripDotP/$tripDotR/g; s/$stickP/$stickR/g'

Solution

  • This sed isn't going to work:

    sed 's/"$ampP"/"$ampR"/g'
    

    because of wrong shell quoting. Your shell variables won't be expanded at all in single quotes. Try using this form:

    sed "s~$ampP~$ampR~g"