1

I have a variable in a Bash script, and I want to replace all occurrences of / in it with _ and all occurrences of + with -; and I want to remove all occurrences of =. So, if this were JavaScript, something like this:

str = str.replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");

How can I do this in Bash.

ruakh
  • 175,680
  • 26
  • 273
  • 307
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • this is a dupe: http://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script – Hitmands Dec 10 '15 at 18:15
  • 1
    @Hitmands Not strictly a dupe, as this question is about character translation (`tr` territory) whereas the proposed dupe is about longer strings (`sed` territory, though certainly the `y%/+/_-/` command of `sed` could come in handy here as well). – tripleee Dec 11 '15 at 14:11

2 Answers2

1

You can do this in BASH:

s='my/String+One=Two'
s="${s//\//_}"
s="${s//+/-}"
s="${s//=/}"
echo "$s"
my_String-OneTwo
anubhava
  • 761,203
  • 64
  • 569
  • 643
1
echo "$string" | tr '/+''_-' | tr -d '='
tripleee
  • 175,061
  • 34
  • 275
  • 318