LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-01-2017, 08:19 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Rep: Reputation: 90
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 08:59 AM.
 
Old 08-01-2017, 11:55 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954
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 12:09 PM.
 
2 members found this post helpful.
Old 08-01-2017, 12:22 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,768

Rep: Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797
@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 01:39 PM.
 
Old 08-01-2017, 01:14 PM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
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
 
Old 08-01-2017, 02:07 PM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954
Quote:
Originally Posted by MadeInGermany View Post
@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 02:40 PM.
 
2 members found this post helpful.
Old 08-01-2017, 04:13 PM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
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.
 
Old 08-01-2017, 04:55 PM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954
It's clearly some weird parsing bug in bash as regex='[\.]' ; [[ 'wibble\' =~ $regex ]] && echo yes works
 
Old 08-01-2017, 06:50 PM   #8
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware, Debian, Gentoo, FreeBSD, OpenBSD
Posts: 208

Rep: Reputation: 41
Quote:
Originally Posted by vincix View Post
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.
Old 08-02-2017, 04:43 AM   #9
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 204

Rep: Reputation: 106Reputation: 106
Small perplexment

Why isn't this a syntax error for an unmatched single quote?

Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes
 
Old 08-02-2017, 04:46 AM   #10
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
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.
 
Old 08-02-2017, 05:36 AM   #11
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954
Quote:
Originally Posted by josephj View Post
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 06:17 AM. Reason: expanded.
 
1 members found this post helpful.
Old 08-03-2017, 04:26 AM   #12
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 204

Rep: Reputation: 106Reputation: 106
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.
 
Old 08-03-2017, 04:34 AM   #13
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
I don't get the joke, but I'm never any good at those
 
Old 08-04-2017, 03:03 AM   #14
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 204

Rep: Reputation: 106Reputation: 106
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.
 
Old 08-04-2017, 04:08 AM   #15
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954Reputation: 3954
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash for strange expansion doru Linux - Newbie 6 12-18-2014 02:48 PM
variable expansion in bash coolhandluke1 Programming 4 01-09-2008 03:45 PM
Variable expansion in BASH champak Programming 5 11-26-2007 02:44 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM

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

All times are GMT -5. The time now is 08:55 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