LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-12-2015, 10:08 PM   #1
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Rep: Reputation: Disabled
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!
 
Old 10-12-2015, 10:30 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
Old 10-12-2015, 10:43 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
What he said ^ ..
Another example would be the use of eg '*' in a find cmd; you need the wildcard char to be parsed by the find cmd, not the shell.
 
Old 10-12-2015, 11:14 PM   #4
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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?
 
Old 10-12-2015, 11:25 PM   #5
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
What he said ^ ..
Another example would be the use of eg '*' in a find cmd; you need the wildcard char to be parsed by the find cmd, not the shell.
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?
 
Old 10-13-2015, 12:22 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by glenjoker View Post
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.
 
Old 10-13-2015, 12:30 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by glenjoker View Post
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).
 
Old 10-13-2015, 12:48 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@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

Last edited by chrism01; 10-13-2015 at 12:50 AM.
 
Old 10-13-2015, 01:04 AM   #9
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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.
 
Old 10-13-2015, 01:07 AM   #10
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
@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.
 
Old 10-13-2015, 07:54 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
No worries; you're welcome
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use grep to grab a value in between character with a single command :/ Drigo Linux - Newbie 3 11-14-2013 02:30 PM
[SOLVED] Simple bash question - replacing single quotes in filenames with another character newbash Linux - Newbie 8 08-10-2012 10:17 PM
running a command that contains single quotes with a variable inside babouyes Linux - Server 3 02-29-2012 12:15 PM
grep command and quotes metalenkist Linux - Newbie 4 12-16-2009 05:32 AM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration