Tool : Git Bash for Windows
Problem : Trying to insert the text "\\connect central" at the top of each file in a directory.
Code :
for f in $DIR/*.sql; do
sed -i "1i \\\connect central" $f
done
This does try to edit inline and insert my text, but three backslashes (like I've read everywhere) doesn't create the single backslash like I'm expected. Instead I get:
I've also tried some variants along the lines of:
for f in $DIR/*.sql; do
sed -i -e "1i `\\\connect central`" $f
done
but that throws an error of sed: -e expression #1, char 3: expected \\ after
a', c',
i'`
Use single quotes instead of double quotes. Backslash is an escape character inside double quotes, so you need to double it to pass it through to the sed
command literally. It has no special meaning inside single quotes.
sed -i '1i \\\connect central' "$f"
To do it with double quotes (which you might need if there's variable content in the string you're inserting), you have to double all the backslashes:
sed -i "1i \\\\\\connect central" "$f"
For more information, see Difference between single and double quotes in Bash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.