1

I have a string Tes$123 and would like to replace with Tes\$123. How can I echo the string with the replacement in Unix?

  • Is this string in a file? A shell variable? A filename? – Wildcard Jul 11 '17 at 21:37
3
$ foo='Tes$123'
$ echo ${foo/$/\\$}
Tes\$123

We replace here the $ with \$, but because \ is special, we have to escape it with a \ of its own.

The construct ${haystack/pin/needle} will replace the first occurrence of 'pin' with 'needle' in the variable haystack. To replace all occurrences of 'pin', you can use ${haystack//pin/needle}.

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.