0

I want to use sed to strip the leading "./" from multiple strings in a bash script. I tried this:

search='\.\/'; replace=''; sed -i '' "s#$search#$replace#" ${MYVAR}/script.sh

but it didn't work. The command exited without errors, but the "./" was not removed.

However, running the same bash command twice in a row worked.

If anyone can help clear this up it would be really helpful, i.e. why the command fails when run once, but works when run twice in a row

abra
  • 153
  • 2
  • 10
  • How does it fail? What exactly happens when you run it once? – Benjamin W. Sep 04 '20 at 14:18
  • The command exits without errors, but the "./" has not been removed. I'll edit my question to mention that. – abra Sep 04 '20 at 14:18
  • 1
    Can you show representative input and what it looks like after the first and second attempt? – Benjamin W. Sep 04 '20 at 14:24
  • Works for me. Demo: https://ideone.com/nC9BDY – tripleee Sep 04 '20 at 14:24
  • Works for me too when using a string as input like in your example. Also works with a really minimal example file. Maybe there's something else in my script that's interfering somehow. – abra Sep 04 '20 at 14:27
  • 1
    The command you're running only replaces the first occurrence on each line. Try using the "global" flag on the sed substitute command. `"s#$search#$replace#g"` – Eric Bolinger Sep 04 '20 at 22:34
  • It works, thank you very much! Looks like the "g" fixed it. – abra Sep 07 '20 at 07:55

0 Answers0