I have a command that sets an environment variable from a command like this:

BLACKLIST=$(python tools.py gen-blacklist)

Which results in a string that contains dots * and asterisks *, like this:


I want to be able to escape all the dots and asterisks in the string, this is what I have tried:

TEMP=$(python tools.py gen-blacklist) && BLACKLIST=$(echo ${TEMP/.\*/\\.\\*}) && echo $BLACKLIST

but it only replaces at the first occurence.


How do I escape for all occurences of * and .?


2 Answers 2


Smells like an xy problem, but regardless...

From parameter expansion, this is what the manual has to say on ${parameter/pattern/string}-style expansions -

If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced

Therefore ${TEMP/.\*/\\.\\*} needs to change to ${TEMP//.\*/\\.\\*} (notice the additional / immediately after TEMP).

This yields LISTEN,UNLISTEN,NOTIFY,SHOW,REFRESH,pg_notify,\.\*remove,\.\*delete,\.\*update,\.\*create,\.\*insert


Quote more (™) ....


TEMP=$(python tools.py gen-blacklist) &&
echo "$TEMP"

If you really need the dots and asterisks escaped, use (in bash):

TEMP=$(python tools.py gen-blacklist) &&
BLACKLIST=${TEMP//\.\*/\\.\\*} &&

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.