Home > OS >  Replace literal * in variable to another value
Replace literal * in variable to another value

Time:01-27

I'm trying to replace a literal * in a string, with a $variable. I have tried these variations:

sed "s/*/$domain/g" <<< $exlusion
sed "s/\*/$domain/g" <<< $exlusion
sed "s/\\*/$domain/g" <<< $exlusion

I've even tried:

$exlusion = sed "s/\\*/$domain/g" <<< $exlusion

and

exclusion="${exlusion//\\*/$domain}"

However, the * never gets replaced. What am I doing wrong? It works fine when I test the same logic in SSH:

sed "s/\\*/foo/g" <<< *:public_html
foo:public_html

CodePudding user response:

You don't need sed. Using shell parameter expansion:

exclusion='*:public_html'
domain=foo

echo "${exclusion/\*/$domain}"

should work.

  •  Tags:  
  • bash
  • Related