I was playing around learning linux commands when I noticed this weird unusual behaviour of the "grep" command . In the /usr/bin
directory , when I run :
me@me-desktop:/usr/bin$ ls -l | grep bashb*
-rwxr-xr-x 1 root root 6988 Mar 31 2024 bashbug
which is expected , but if I do the same outside /usr/bin
me@me-desktop:~/Code/linux$ ls -l /usr/bin | grep bashb*
-rwxr-xr-x 1 root root 1446024 Mar 31 2024 bash
-rwxr-xr-x 1 root root 6988 Mar 31 2024 bashbug
-rwxr-xr-x 1 root root 4527 Apr 17 2023 dh_bash-completion
lrwxrwxrwx 1 root root 4 Aug 20 23:14 rbash -> bash
As you can see it now optionally ignores the last b
in bashb
.
This happens in many other cases as well . For e.g. if you do apt list | grep lib*
, then it lists out the ones with li
as well.
bashb*
does globbing in ls -l | grep bashb*
and matches the file bashbug
. Your grep
effectively becomes
/usr/bin$ ls -l | grep bashbug
When your current directory does not contain a matching file, bashb*
is used as-is, which is also wrong since it will match any file with bash
anywhere in it its name, followed by 0 or more b
's.
Note that globbing and regex follow different rules.
bashb*
in globbing means a file starting with bashb
and can then have any number of characters after it.bashb*
in regex matches bash
anywhere in the string and b*
means the character b
0 or more times. foobashbbbbbar
would match that for example.grep
ing in the output from ls
is generally not a good idea.