ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Putting single quotes around the input should do the trick. It, using single quotes, prevents bash from doing anything with the characters, where putting double (or no) quotes around the input doesn't.
There are several other regex character classes available. There's a section in the grep info page that documents all of them and what characters they cover.
The real point of the above solution though is not the character class, but the character range construct, which allows you to test for any of a list of individual characters. All a character class is is just a pre-defined range of characters.
As an example, to test for only a subset of punctuation marks, plus spaces and tabs, plus digits, you could use this:
Code:
grep -q "[./:;<=>?[:blank:][:digit:]]" testfile && echo found it
The bash [[ extended test can also handle regex, of course. You could use this to test for the existence of punctuation in input arguments, for example:
Code:
re='[[:punct:]]'
for arg in "$@"; do
[[ $arg =~ $re ]] && echo "found punctuation in [$arg]"
done
It's usually best to store the regex pattern in a separate variable first. Otherwise you'll have to worry about escaping shell-reserved characters.
By the way, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
Last edited by David the H.; 09-27-2011 at 05:19 PM.
Reason: added additional info
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.