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
  • 320,670
  • 36
  • 633
  • 936
Chandani Patel
  • 208
  • 3
  • 9
  • [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/q/131766/170373), https://mywiki.wooledge.org/Quotes – ilkkachu Jun 01 '18 at 08:38

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
  • 320,670
  • 36
  • 633
  • 936
  • 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