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
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
's/this\.NAME=""/this.NAME="'"$name"'"/g'
Broken down into parts:
's/this\.NAME=""/this.NAME="'
"$name"
, this is the important bit: the variable expansion must be quoted.'"/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:
'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