3

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.

5

escape slash with backslash

echo ${test//\//:}
8

This:

${test//\/:}

would replace all instances (since double-slash // in the start) of /: with nothing (no second unescaped slash).

This:

${test/\//:}

Would replace the first instance (since a single slash as separator) of / (which was escaped) with :.

And this:

${test//\//:}

Should replace all matches of / with a :.

Example:

$ test="/this/isjust/atestvariable/:for/:stringoperation"
$ echo ${test//\/:}
/this/isjust/atestvariableforstringoperation
$ echo ${test/\//:}
:this/isjust/atestvariable/:for/:stringoperation
$ echo ${test//\//:}
:this:isjust:atestvariable::for::stringoperation

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.