[SATLUG] character classes with *

Wes Henderson whendersonii at gmail.com
Thu Oct 2 14:44:17 CDT 2014


Sorry for not addressing the question, I hope the examples help
none-the-less; feel free to hit me up off list if you like. As for the
question, Bruce is correct that Bash is interrupting the special characters
and quoting is needed. As for the examples that worked without quotes, I
would advise that you use the quotes anyway as doing so is POSIX compliant
and thus you can ensure that your command/script will work between
environments (being explicit is never a bad thing).

Lets do some testing just to be 100% sure what is happening. To do this
lets run the same command in a new bash instance in debug mode:

[wes at localhost bash-test]$ bash -x -c 'grep [a-z]\* test'
+ grep '[a-z]*' test
aaLLyLayLaaya*

[wes at localhost bash-test]$ bash -x -c 'grep "[a-z]*" test'
+ grep '[a-z]*' test
aaLLyLayLaaya*

[wes at localhost bash-test]$ bash -x -c 'grep [a-z]* test'
+ grep test test

As you can see the third example did NOT work and bash interrupted the
command to be 'grep test test'. Odd. Lets make some new files and test some
more:

[wes at localhost bash-test]$ touch test2 test3 another_file

[wes at localhost bash-test]$ bash -x -c 'grep [a-z]* test'
+ grep another_file test test2 test3 test

So it is pretty clear that bash, rather than grep, is interrupting the
special characters. I believe that the big take away here should be that
being explicit is the safest and most reliable way to achieve your goal.

[wes at localhost bash-test]$ python -c 'import this'|grep Explicit
Explicit is better than implicit.

http://www.gnu.org/software/bash/manual/bashref.html#Double-Quotes


More information about the SATLUG mailing list