Bash – Replacing a pattern with a string

bashstring

Suppose we declare

test="/this/isjust/atestvariable/for/stringoperation"

and we want to replace each instance of '/' with the colon ':'.

Then, I think this command should work:

echo ${test//\/:}

(as ${variable//pattern/string} replaces all matches of the pattern with the specified string )

But, on running echo ${test//\/:}, I get the output as

/this/isjust/atestvariable/for/stringoperation

Where I could be going wrong? Thank you for your help.

Best Answer

escape slash with backslash

echo ${test//\//:}
Related Question