I’m trying to write a bash script that can edit itself. The line of code I’m trying to use is:
sed "s/$STRING_TO_REPLACE/$NEW_STRING/" < $0 > $0
But after running the script I end up with an empty file. Any suggestions?
I am creating a script that has two variables that contain default values. The only problem is, these default values will be different for each of the many stations where it will be used. I would rather none of the people running these stations had to open the script and edit the defaults themselves, so I want to include the ability to change the defaults in the script. One of the requirements I have is that the script must be self contained; no other files are to accompany the script and there are to be no environment variables. This would require the script to be able to edit itself.
If you want to do an in-place substitution with sed, use the
-iswitch.I wouldn’t recommend re-writing the currently executing script on the fly. I’m not sure whether it’s possible or not, but if it is, it would certainly be interesting (read: difficult) code to maintain 🙂