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.

2
  • this is a dupe: stackoverflow.com/questions/13210880/… Commented Dec 10, 2015 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). Commented Dec 11, 2015 at 14:11

2 Answers 2

1

You can do this in BASH:

s='my/String+One=Two'
s="${s//\//_}"
s="${s//+/-}"
s="${s//=/}"
echo "$s"
my_String-OneTwo
Sign up to request clarification or add additional context in comments.

Comments

1
echo "$string" | tr '/+''_-' | tr -d '='

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.