I want to replace windows server paths (\\\aaa\bbb\ccc\) with linux paths /foo/bar/ on all occurrences inside all .txt files in a folder (/xxx/yyy/zzz/).

For that I made a loop that does not seem to work either on bash or tcsh (using foreach).

for f in $FILES
        sed -i 's+\\\aaa\bbb\ccc\+/foo/bar/+g' $f

The error I get is the following:

sed: -e expression #1, char 133: unterminated `s' command

I am guessing the issue must be in how sed handles these tricky characters but I cannot identify it.

Thank you in advance.

1 Answer 1


\ is an escaping operator in regexp syntax (and many other things), so needs to be escaped itself, along with . which is also special there. Here, the \ in front of + caused the + not to be taken as the delimiter in s+pattern+replacement+flag.

You also don't need the loop nor bash.

#! /bin/sh -
sed -i 's|\\\\100\.100\.0\.0\\aaa\\bbb\\ccc\\|/foo/bar/|g' /xxx/yyy/zzz/*.txt

If you wanted to use bash and a variable, you'd rather use an array:

#! /bin/bash -
shopt -s nullglob
files=( /xxx/yyy/zzz/*.txt )
if (( ${#files[@]} > 0 )); then
  sed -i -- 's|\\\\100\.100\.0\.0\\aaa\\bbb\\ccc\\|/foo/bar/|g' "${files[@]}"

Though if you're going to use a shell other than sh, bash is a relatively poor choice (not as poor as (t)csh of course which shouldn't be used in this century). Several shells including zsh, ksh93 or fish would make it easier and cleaner.

  • This works perfectly! Thank you very much, I will familiarize myself with other shells.
    – CodingPear
    Jan 30 at 20:07

You must log in to answer this question.

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