1

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:

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

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.

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

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

1

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

1

Quote more (™) ....

Try:

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//\.\*/\\.\\*} &&
echo "$BLACKLIST"

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.