2

I want to replace string of all js files exsting in directories

export name="test_user" &&
echo $customerName &&
sed -i -- 's/this.NAME=""/this.NAME=\"'$name'\"/g' *.js

If I don't use space it works fine but with string above command fails

Kusalananda
  • 333,661

1 Answers1

2
's/this\.NAME=""/this.NAME="'"$name"'"/g'

Broken down into parts:

  1. 's/this\.NAME=""/this.NAME="'
  2. "$name", this is the important bit: the variable expansion must be quoted.
  3. '"/g'

Also, if you use \" in the replacement, and it's in single quotes, then you would insert \", not ", into the result.

You also seem to use a variable called customerName. I'm assuming this is a typo.

Whether the sed that you are using can do in-place editing in the way that you seem to want to do, I don't know.

Related:

Kusalananda
  • 333,661
  • Whether 's/x/\"/' replaces x with " or \" or anything else is unspecified by POSIX but in practice I find that most implementations (all those I tried) replace x with ", not \". – Stéphane Chazelas Jun 01 '18 at 09:58