Contents
How to escape forward slashes in sed command?
Closed 2 years ago. With Bash and SED I’m trying to replace two strings in a js file with URL’s. The two urls that should be inserted is input params when I run the .sh script. However to make this usable in my sed command I have to escape all forward slashes with: \\\\ ?
Why does backslash have to be escaped in a shell?
Backslash ( \\) is special because it is itself used to mark other characters off; we need the following options for a complete shell: So backslash itself must be escaped to show that it is to be taken literally. The other special character, the backtick, is discussed later in Chapter 12, External Programs .
What are the characters in the escape script?
6. Escape Characters Certain characters are significant to the shell; we have seen, for example, that the use of double quotes (“) characters affect how spaces and TAB characters are treated, for example: So how do we display: Hello “World” ?
What do I need to escape when using SED in a SH?
You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or \\~/dir~p; you’ll need a backslash before the delimiter if you want to include it in the BRE.
How to pass variable containing backslashes to SED?
Another way of doing it, although uglier than anubhava’s answer, is by escaping all the backslashes in var using another sed command: Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error.
Are there any special characters in BSD sed?
While this may be the case, some special characters have limited or no support at all on BSD sed, such as ‘|’, ‘?’, and ‘+’, as it more closely adheres to the POSIX syntax standards. The inclusion of those characters, in a fashion similar to that of GNU sed, will often result in issues with portability and functionality of scripts utilizing sed.