Reputation: 954
I have a bash script in which I invoke sed to perform some manipulations on a text files contents. However I have noticed that if the filepath contains certain characters they are interpreted by sed as an instruction instead of a literal string. How can I escape these characters from the bash script?
The part of the script in question (where $I is actually declared as an increment used inside a loop):
$I=0
OUTPUT=folder/m_Filename"$I".h
sed -i '1s;^;TEXT_TO_PREPEND;' "$OUTPUT"
which throws:
sed: 1: "{folder/m_Filename0.h}": invalid command code m
Running the script on OS X El Capitan.
Upvotes: 0
Views: 197
Reputation: 74645
Some versions of sed require that you specify a suffix after the -i
option, which will be used to create a backup of the file you have overwritten:
sed -i '.bak' '1s;^;TEXT_TO_PREPEND;' "$OUTPUT"
Without this, it's possible that the sed command is being interpreted as the backup suffix, in turn causing the filename to be interpreted as the sed command.
If you really don't want a backup to be created, then you can pass an empty string -i ''
.
Upvotes: 3