I want replace / from the path /test/path/to/replace

i.e. the expected output should be =test=path=to=replace

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented Nov 18, 2022 at 15:09
  • 1
    Recommend reading How do I ask a good question? -- What have you tried so far? What were the results? While your question can probably be answered as it is, it would be better if you let us know what you've tried already so that no one wastes their time and yours providing an answer that already hasn't worked for you. Thanks! Commented Nov 18, 2022 at 15:21
  • Additionally: where does this path come from? From a regular file? From a program that prints it to stdout? Or is it stored in a shell variable? Commented Nov 18, 2022 at 15:46

3 Answers 3

#! /usr/bin/env bash

echo "$B"



Here are a couple of options. You can use tr

new_path=$(echo '/test/path/to/replace' | tr '/' '=')

or sed

new_path=$(echo '/test/path/to/replace' | sed 's/\//=/g')


Sed command can be used here.

sed "s/\//=/g"

Command output examples:

pwd | sed "s/\//=/g"
echo "/test/path/to/replace" | sed "s/\//=/g"

File example (change "/" to "=" in text inside a file):

sed "s/\//=/g" file_name

Variable example (change content of variable; "/" to "="):

echo $var1 | sed "s/\//=/g"

Of course you can substitute "default" separator (slash sign "/") for, say minus sign "-"; in that case you avoid using backslash sign and get better readability:

sed "s-/-=-g"

Explanation (if some of you are not so familiar with sed command):

sed "s/\//=/g"
sed - stream editor (non-interactive command-line text editor)
s   - substitute sed command
/   - separators - first, third and fourth slash sign
\   - used for escaping second slash sign so that bash interprets second slash sign as litteral slash sign - not as separator)
/   - second slash sign; litteral slash sign (not separator) - term that we want to replace
=   - term that we want to get after replacement
g   - global sed command (replace all slashes, not just first one - without 'g' sed will replace only first slash in the path)

sed "-/-=-g"
All the same as in the previous explanation, except the two things:
1. We don't use escape sign (backslash) and
2. minus signs represent separators.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .