vsz
vsz

Reputation: 4909

What does a backslash followed by a grave accent mean in regular expressions?

I've found the following in a regular expression:

s/.*\`\(.*\)'/\1/g

I'm wondering what the grave accent means. I have only a very basic understanding of regular expressions, so I tried to look it up, but a basic search didn't lead me to anything useful.

The context: I'm trying out the accepted answer to this question: An error building Qt libraries for the raspberry pi

It suggests running the following script to adjust simlinks:

find . -maxdepth 1 -type l | while read i;
do qualifies=$(file $i | sed -e "s/.*\`\(.*\)'/\1/g" | grep ^/lib)
if [ -n "$qualifies" ]; then
newPath=$(file $i | sed -e "s/.*\`\(.*\)'/\1/g" | sed -e "s,\`,,g" | sed -e "s,',,g" | sed -e "s,^/lib,$2/lib,g");
echo $i
echo $newPath;
sudo rm $i;
sudo ln -s $newPath $i;
fi
done

However, it doesn't do anything, so I'm trying to understand how it is supposed to work.

The find finds a lot of files, an example:

./libcap-ng.so.0
./libnettle.so.4
./libSDL-1.2.so.0
./libwebpmux.so.1
./libnss_nisplus.so
./libavahi-gobject.so.0
./libgdk-3.so.0
./librsvg-2.so.2
./libatk-bridge-2.0.so.0
./libgeoclue.so.0
./libdv.so.4
./libcdda_interface.so.0
./libheimbase.so.1
./libfontconfig.so.1
./libXinerama.so.1
./libopenal.so.1
./libicule.so.52 
./libaudio.so.2
./libmplex2-2.1.so.0

and so on for dozens of other files, but none of them seem to "qualify" for the regex.

Upvotes: 2

Views: 663

Answers (1)

ruakh
ruakh

Reputation: 183341

In Bash, double-quoted strings "..." allow various expansions, including command substitution with $(...) or `...`. For example, this:

cat "fo`echo ob`ar"

is equivalent to cat foobar, because `echo ob` means "run the command echo ob and plop its output here".

So if you want to include an actual ` in a double-quoted string, you have to quote it with a backslash. And that's what your script is doing. sed -e "s,\`,,g", for example, just means sed -e 's,`,,g', i.e. "remove all occurrences of `". (Note that the ` doesn't mean anything special to sed itself; it just matches ` in the input. The same command could have been written as tr -d '`'.)

Upvotes: 4

Related Questions