The precarious nature of the Wildcards as demonstrated on my Mini

Working with wildcards today in class was a bit baffling especially before tackling RegEx. Starting off with a text directory of sorts, the range operators became particularly challenging. A little bit of history on Wikipedia reveals a few tricks:

! vs ^

In Unix shells, characters inside square brackets: [ and ] match a single character set. When you use a leading !, it negates the set and matches a character not within the list. In shells where the ! is used for something else (like quickly pulling up a line in history), the ^ operator has also been used as a character for negation. This is the brief history for how ^ came about to replace the ! operator.

For those of you into that little trick available in linux, history basically reveals a list of the commands that you've typed at the command line with a number printed before it:

  562  ls
  563  ls [!a-z0-9]*
  564  ls [!a-z0-9]*
  565  ls [0-9^A-Z]*
  566  ls
  567  python
  568  ls
  569  ls [^0-9A-Z]*
  570  ls [!a-z0-9]*
  571  ls [0-9^A-Z]*
  572  history

Then, you can type !571 to run a shortcut command. As a result of this implementation, the ^ was also employed to mean a negation. But! there's some weird stuff going on. Running the following command should return everything that starts with neither a capital letter or a number:

ls [!a-z0-9]*

As shown above, it does.

Replacing the ! with a ^ results in the exact same thing:

ls [^a-z0-9]*

Things get weird when putting the ! in the middle of the two sets:

ls [0-9!A-Z]*

This results in "event not found". Interchangeably using ^ with ! actually results in the ^ behaving as an or operator returning all files that have either a 0-9 or an A-Z.

ls [0-9^A-Z]*

How precarious!!

Graces-Mac-mini:wildcards gracewoo$ ls [0-9!A-Z]*
-bash: !A-Z]*: event not found
Graces-Mac-mini:wildcards gracewoo$ ls [0-9^A-Z]*
04Apr	2004	Barbie	Cut	Kola	YuMMy

Keep in mind, these wild cards are not actually expanded by the commands ls and so forth, but rather by the shell itself.

Written byadmin

Documenting makes the mundane seem interesting. Interesting matters seem to demand attention on their own.