The final solution will be this one:
$ sed 's:/:\\/:g' <<<"$str"
\/tmp\/test\/folder1\/test.txt
But to explain how to get there:
Yes, you were missing one backslash:
$ str='/tmp/test/folder1/test.txt'
$ sed "s/\//\\\\\//g" <<<"$str"
\/tmp\/test\/folder1\/test.txt
I hope that one space will make it clear:
$ sed "s/\//\\\\ \//g" <<<"$str"
\ /tmp\ /test\ /folder1\ /test.txt
But, if you were to change the sed delimiter to :
(for example):
$ sed "s:\/:\\\\\/:g" <<<"$str"
\/tmp\/test\/folder1\/test.txt
But that is not strictly correct as the (now not special) /
does not need scaping:
$ sed "s:/:\\\\/:g" <<<"$str"
\/tmp\/test\/folder1\/test.txt
And, if you were to use single quotes instead of double quotes the shell will not change double \\
to one, so less \
will be correct:
$ sed 's:/:\\/:g' <<<"$str"
\/tmp\/test\/folder1\/test.txt
Which is quite cleaner.
/
for\/
(so replace all\/
with/
) or do you want to substitute\/
for/
(replace all/
with\/
)?