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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
08-01-2017, 09:19 AM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Rep:
|
expansion in bash
There's this statement that I've come across in Advanced Bash Scripting (Inserting a blank line between paragraphs in a text file):
Code:
if [[ "$len" -lt "$MINLEN" && "$line" =~ [*{\.}]$ ]]
I don't know exactly how to interpret the bolded part. What is the meaning of the curly brackets in this context? Does it simply mean everything ending in a dot?
Last edited by vincix; 08-01-2017 at 09:59 AM.
|
|
|
08-01-2017, 12:55 PM
|
#2
|
LQ Veteran
Registered: May 2008
Posts: 7,102
|
It's somewhat confusing because many of those characters have special meaning in an extended regex, however, they're all used within a square brackets (list specification) so they're treated as literals (with the exception of the '.' which still has special meaning in a list and needs to be escaped).
So, unless I'm reading it wrongly, it will match any line that ends with any of '*' '{' '}' or '.' characters.
edit: actually, I got that wrong. The \ and . are both literal too and it isn't an escape. God I hate reading regexs!
Last edited by GazL; 08-01-2017 at 01:09 PM.
|
|
2 members found this post helpful.
|
08-01-2017, 01:22 PM
|
#3
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,010
|
@GazL, IMHO a dot in a [character set] is a literal dot. No need for escaping it; the preceding backslash does nothing and can be omitted.
BTW a literal backslash in a [character set] DOES need another backslash.
Last edited by MadeInGermany; 08-01-2017 at 02:39 PM.
|
|
|
08-01-2017, 02:14 PM
|
#4
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
I don't know why I interpreted in such a complicated way. Now I understand that those are literal characters. I thought it was some special notation. Thank you
|
|
|
08-01-2017, 03:07 PM
|
#5
|
LQ Veteran
Registered: May 2008
Posts: 7,102
|
Quote:
Originally Posted by MadeInGermany
@GazL, IMHO a dot in a [character set] is a literal dot. No need for escaping it; the preceding backslash does nothing and can be omitted.
BTW a literal backslash in a [character set] DOES need another backslash.
|
I'd already realised my mistake about the dot and corrected my post, but \ doesn't need escaping within a regex list element (other than any escaping it might need to stop the shell interpreting it), as can be seen here:
e.g.
Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
test@ws1:~$ [[ 'wibble\' =~ [\\.]$ ]] && echo yes
yes
test@ws1:~$ echo wibble\\ | egrep '[\.]$'
wibble\
As can be seen from the grep example, it's the shell that requires the additional escape not the regex pattern itself. Interestingly, [[ .... =~ '[\.]$' ]] doesn't work however. which I find surprising.
Oh, and don't you just love consistency:
bash:
Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
test@ws1:~$
ksh:
Code:
$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
yes
$
Last edited by GazL; 08-01-2017 at 03:40 PM.
|
|
2 members found this post helpful.
|
08-01-2017, 05:13 PM
|
#6
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Related to this are the following lines in the script:
Quote:
# if [[ "$len" -lt "$MINLEN" && "$line" =~ \[*\.\] ]]
# An update to Bash broke the previous version of this script. Ouch!
# Thank you, Halim Srama, for pointing this out and suggesting a fix.
|
I'm also surprised that simple or double quotes don't work at all. I don't understand the reason why. I've also tried it in ksh, but it seems to behave the same.
|
|
|
08-01-2017, 05:55 PM
|
#7
|
LQ Veteran
Registered: May 2008
Posts: 7,102
|
It's clearly some weird parsing bug in bash as regex='[\.]' ; [[ 'wibble\' =~ $regex ]] && echo yes works
|
|
|
08-01-2017, 07:50 PM
|
#8
|
Member
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware, Debian, Gentoo, FreeBSD, OpenBSD
Posts: 208
Rep:
|
Quote:
Originally Posted by vincix
Related to this are the following lines in the script:
I'm also surprised that simple or double quotes don't work at all.
|
The reason is because the =~ token expects a regular expression to follow. If you quote it, the expression becomes a string which is then compared against, and fails.
|
|
2 members found this post helpful.
|
08-02-2017, 05:43 AM
|
#9
|
Member
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214
Rep:
|
Small perplexment
Why isn't this a syntax error for an unmatched single quote?
Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
|
|
|
08-02-2017, 05:46 AM
|
#10
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
I like how a seemingly simple question produced so much interesting stuff! I'm also interested in your question, josephj. What makes it more weird is that if you place the single quote in the regex between the square brackets, you do have to escape it and it only works that way.
|
|
|
08-02-2017, 06:36 AM
|
#11
|
LQ Veteran
Registered: May 2008
Posts: 7,102
|
Quote:
Originally Posted by josephj
Why isn't this a syntax error for an unmatched single quote?
Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
|
Within single quotes all characters are literal: the escape character loses its meaning.
Things get a little more complicated within double-quotes and different shell implementations can vary with how they interpret the escape character: it will depend on what follows it. Basically it's all a bit of a mess.
Last edited by GazL; 08-02-2017 at 07:17 AM.
Reason: expanded.
|
|
1 members found this post helpful.
|
08-03-2017, 05:26 AM
|
#12
|
Member
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214
Rep:
|
Regexes! Can't live with them. Can't live without them.
Regexes make me dizzy. I'm gradually getting better at them over the years, but I'm still a beginner.
I even read a whole O'Reilly book on them once and still didn't absorb much.
I can't find the actual strip at the moment, but User Friendly summed it up nicely:
Quote:
Upskilling Strip Date:Jun 28, 2007
Miranda, Sid
-
Sid: Hey, Miranda. Busy?
Miranda: Just trying out some new skills.
Miranda: I've been experimenting a lot to push the boundaries of my knowledge.
Sid: I'd say you've come a long way. Appraoching mastery, even.
Miranda: You really think so?
Sid: Well, for one thing I can't tell if that's a regex or line noise.
|
|
|
|
08-03-2017, 05:34 AM
|
#13
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
I don't get the joke, but I'm never any good at those
|
|
|
08-04-2017, 04:03 AM
|
#14
|
Member
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214
Rep:
|
The bad old days ...
It doesn't work so well without the cartoon (he's looking over her shoulder at her CRT terminal) - and in the bad old days, we used 300 baud modems (and 110 before that - divide by roughly 10 for characters per second) to dial into time sharing systems. If the line quality was low, you'd get garbage characters sent or received (generated by noise on the transmission lines - the encoding used analog tones) - which don't look that different from a typical regex.
|
|
|
08-04-2017, 05:08 AM
|
#15
|
LQ Veteran
Registered: May 2008
Posts: 7,102
|
The realisation that there are generations out there that have never experienced serial terminals and line noise and need this joke explaining to them suddenly makes me feel very old.
|
|
|
All times are GMT -5. The time now is 04:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|