0

When I put single quotes around backslash like this

echo '\'

it prints the backslash, it does not care for escaping.

but when I use the same thing in sed like this

sed 's/\/a/g' : trying to replace backslash with a

or in case of grep

echo '\\//' |grep '\', it does not work and it requires escaping as well.

why does this backslash need to be escaped? I mean inside all of these lose their meaning right?

4

1 回答 1

3

The problem with backslash is that it has many special meanings, for multiple tools and multiple contexts.

Within single quotes, it loses special meaning for your shell (please learn to tag your questions with its name when it's relevant or when you're unsure), but it retains special meaning for sed (it is special for regular expression syntax, so it's expected for sed and grep)

Usually you can escape a backslash instead of quoting it. It doesn't necessarily make things readable, but in some contexts, it can. That's how I grep a literal \ without quotes:

grep \\\\

Shell turns \\\\ into \\, and \\ in regular expression means literal \.

于 2013-02-10T12:35:31.843 回答