Ubuntu – apt-file regex: escaping a dot

aptapt-filebashperlregex

I want to find all packages which provide a shared object containing "ssl" in the file name. The apt-file manpage says:

–regexp | -x
Treat pattern as a (perl) regular expression. See perlreref(1)
for details. Without this option, pattern is treated as a
literal string to search for.

Therefore my first try was

apt-file search --regexp .*ssl.*\.so.*

This, however, gave me results such as

witty-examples: /usr/lib/Wt/examples/feature/client-ssl-auth/resources

which has no ".so" in the file name.

I played around a bit and came up with

apt-file search --regexp .*ssl.*\\.so.*

i.e. I escaped the backslash which was supposed to escape the dot. This gave me the results I wanted.

Could someone explain why I need a double backslash in this case?

Best Answer

You need a double backslash \\ because the single backslash is not only the regex escape character but also the one your shell uses. E.g. you escape the dot, which on shell level just interprets to a regular dot, that is then passed to apt-get and machtes every character (as a regular dot usually does).

So the answer is, first the string is interpreted by the shell, where the backslash has one meaning, and after that, it is passed to apt-get. The regex-engine of apt-get then interpretes the preprocessed string again. There the backslash has been replaced (and also has a slightly different meaning).

Another solution is to put the regex in quotes as in:

apt-file search --regexp '.*ssl.*\.so.*'
Related Question