0

Hello I need to replace a string but i receive error using string 'return false;'

#!bin/bash
oldstring='{alert("bash")}'
newstring='{return false;}'
grep -rl $oldstring /home/commons.bundle.js | xargs sed -i s/$oldstring/$newstring/g

error: sh deleteBoo.sh sed: espressione -e #1, carattere 25: comando `s' non terminato

1 Answers1

2
grep -rlZF -- "$oldstring" /home/commons.bundle.js |
xargs -r0 sed -i "s/$oldstring/$newstring/g" -- 

The following minor changes should make it work:

  • grep should use -F so that the strings passed are not evaluated by grep as regexes.
  • Another grep option to use is -Z which will pass filenames onto xargs using the null delimiter.
  • At the receiving end, xargs with -0 will unpack safely the filenames now. Nulls cannot occur in filenames by `law'
guest_7
  • 5,728
  • 1
  • 7
  • 13