1

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

1

1 Answer 1

1
'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:

1
  • 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 \". Jun 1, 2018 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.