I was explaining the pattern matching in the shell earlier today, and used a very unfortunate example for explaining the "exactly one" pattern:
if [[ $var == @([0-9])% ]]; then...
I have (correctly) explained the above pattern as exactly one digit followed by a percent sign. One of the delegates asked me: why use the fancy characters? Wouldn't the following:
if [[ $var == [0-9]% ]]; then...
be the same? And if so, what's the point of the additional characters! Got me going for a moment! The answer is in the poorly chosen example. Although it works, one would never use the 'exact one' counting in relation to a single pattern specification.
The @(...) notation is used when you need to 'count' alternative patterns, as in:
if [[ $var == @(+|-)[0-9] ]]; then...
Without the @ character, the brackets needed for enveloping the alternative patterns would not work:
if [[ $var == (+|-)[0-9] ]]; then...
ksh: syntax error: '==' missing second argument
And without the brackets altogether:
if [[ $var == +|-[0-9] ]]; then...
gives:
ksh: syntax error: '|' unexpected operator/operand
Twitter Updates
G-AVLN in front of her home
Mostly Unix and Linux topics. But flying might get a mention too.
Subscribe to:
Post Comments (Atom)
Blog Archive
- October (1)
- June (1)
- April (2)
- February (3)
- June (1)
- March (1)
- August (3)
- July (2)
- June (1)
- March (1)
- June (3)
- May (1)
- April (5)
- February (1)
- January (5)
- October (1)
- September (3)
- July (4)
- June (5)
- April (3)
- March (1)
- February (3)
- January (3)
- October (7)
- August (2)
- July (3)
- May (1)
- November (4)
- October (1)
- September (1)
- August (2)
- July (2)
- June (3)
- May (3)
- April (2)
- March (2)
- February (3)
- January (1)
- December (2)
- November (1)
- October (6)
- September (6)
- August (1)
- July (2)
- June (8)
- May (3)
- April (4)
- March (3)
1 comment:
How about:
[+-][0-9]
Better yet, use Bash 3.0 with Extended Regular Expressions
Post a Comment