[SOLVED] Why character classes need to be enclosed within single quotes in grep command?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Why character classes need to be enclosed within single quotes in grep command?
As stated, if people wanted to search for all the letters whether in lowercase or uppercase in a file name "FILE", they would type command
grep '[a-zA-Z]' FILE
so my question is, as I understand how single quotes work, that any string within a pair of single quotes would be interpreted literally by the shell; therefore, in this case, if I use the aforementioned command, I would expect that the character class [a-zA-Z] would be interpreted literally (i.e. it would not be expanded to abc...z,ABC...Z). Based on this reason, I thought that it would be more sensible if I type command
grep [a-zA-Z] FILE
which apparently did not work out, but why?
Hope you guys can answer me this question. Thanks in advance!
You want grep to interpret the character classes, not the shell. For example, if you have files in your current directory that are named a, b and C, the shell will perform file name expansion and turn
You want grep to interpret the character classes, not the shell. For example, if you have files in your current directory that are named a, b and C, the shell will perform file name expansion and turn
Code:
grep [a-zA-Z] FILE
into
Code:
grep a b C FILE
which is probably not your intention.
Thanks for replying. I just checked, and indeed the output of
Code:
grep [a-zA-Z] FILE
was the same as the output of
Code:
grep a b C FILE
, but what exactly is the behavior of
Code:
grep a b C FILE
? Is this command syntactically correct and is this command actually meaningful? Or does it just output some garbage value?
Thanks for replying. I just checked, and indeed the output of
Code:
grep [a-zA-Z] FILE
was the same as the output of
Code:
grep a b C FILE
, but what exactly is the behavior of
Code:
grep a b C FILE
? Is this command syntactically correct and is this command actually meaningful? Or does it just output some garbage value?
It's a syntactically correct and absolutely legitimate command. Meaning: Print all the lines containing string "a" in files b, C and FILE. If FILE exists, and all files are readable, there won't be an error message.
Thanks for replying, but I still do not get why exactly the string within the single quotes here (i.e. '[a-zA-Z]') was not interpreted literally?
It was interpreted literally by the shell, because of the quotes. It was not interpreted literally by grep, because grep was handed the expression [a-zA-Z] without quoting (as a side remark, grep doesn't use the single quote as quote character).
@glenjoker: try reading this answer https://unix.stackexchange.com/quest...sing-a-command and at least once have a quick read through the linked bashwiki page as well.
You don't necessarily have to memorise the whole thing, but just be aware, and maybe bookmark that answer & refer to it often for while.
PS: a good exercise would be to setup a dev dir for testing stuff and play with the answer; try writing code snippets to check if he's telling the truth
It was interpreted literally by the shell, because of the quotes. It was not interpreted literally by grep, because grep was handed the expression [a-zA-Z] without quoting (as a side remark, grep doesn't use the single quote as quote character).
Yes, I see it now, thanks for the clarification. I really appreciate it.
@glenjoker: try reading this answer https://unix.stackexchange.com/quest...sing-a-command and at least once have a quick read through the linked bashwiki page as well.
You don't necessarily have to memorise the whole thing, but just be aware, and maybe bookmark that answer & refer to it often for while.
PS: a good exercise would be to setup a dev dir for testing stuff and play with the answer; try writing code snippets to check if he's telling the truth
Sure, on it. And thanks for your advice, it means great help to beginners like me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.