Ubuntu – `apt-get` and wildcards: globs vs. regex


Edit: Please note, as written below, I'm aware of answers on this site explaining that apt-get uses regex to interpret packages' names. In fact, the question is directly about a way in which its actual behavior is different from that documented one. Please read the question before suggesting a duplicate.

Some answers on this site warn about using apt-get with wildcards (i.e., asterisks: *), because apt-get supposedly expands them as regular expressions, which might give unexpected (and undesired) results, especially with apt-get remove. Indeed, Ubuntu man page for apt-get reads:

If no package matches the given expression and the expression contains
one of '.', '?' or '*' then it is assumed to be a POSIX regular
expression, and it is applied to all package names in the database.
Any matches are then installed (or removed). Note that matching is
done by substring so 'lo.*' matches 'how-lo' and 'lowest'. If this is
undesired, anchor the regular expression with a '^' or '$' character,
or create a more specific regular expression.

In fact, this answer claims:

apt-get accepts a regular expression and not a glob pattern as the shell.

I believe this is wrong (at least as of Xenial). For example, I can reproduce the following behavior:

$ sudo apt-get install -s 'meld*'
Note, selecting 'meld' for glob 'meld*'

$ sudo apt-get install -s 'meldt*'
Note, selecting 'python-meld3' for regex 'meldt*'
Note, selecting 'python3-meld3' for regex 'meldt*'
Note, selecting 'meld' for regex 'meldt*'

(I didn't remove any matches, only irrelevant parts of apt-get's response.)

It would seem to me, based on this behavior, that apt-get first attempts to match given expressions as globs, and only if it fails, will it then retry as regular expressions.

Do I have that right? Have I misunderstood the man page, or is this behavior badly documented?

Best Answer

  • This is explained in the apt(8) manpage:

    install, remove, purge (apt-get(8))
       Performs the requested action on one or more packages specified via
       regex(7), glob(7) or exact match. The requested action can be
       overridden for specific packages by append a plus (+) to the
       package name to install this package or a minus (-) to remove it.

    This paragraph does not exist in the 15.10 manpage, so it might have been added in 16.04.

    This does not seem to have got a mention in apt's changelog - the commit which added this in 2013 doesn't show any changes in the manpages. This was briefly disabled and reenabled later on (see commits between May and February 2014), and the disabling is mentioned in the changelog.

    So this may have been added four years ago but only documented in 2015. And apt-get's manpage remains neglected.

  • Related Question