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.
"?(pattern-list) Matches zero or one occurrence of the given patterns"
The pattern "b" has 0 occurrences of "a" so based on the above it should have been returned,
Again, why isn't "b" returned?
It matches against the pattern list, so there are zero return or a match, but if it "matches" it only returns matched value in the pattern. Of what purpose/value would there be if you have a list of the whole alphabet and passed it to a pattern to show only 'a' if it showed the full list anyway?
The description just says it matches against the pattern list.
"?(pattern-list) Matches zero or one occurrence of the given patterns"
The pattern "b" has 0 occurrences of "a" so based on the above it should have been returned,
Again, why isn't "b" returned?
because you are looking for a full match, not a partial match. The full string should match the pattern, and actually in your example there is no match.
why should I care it "searches" also empty strings but returns only hits with non-empty ones?. It seems that searching "0 occurrences" makes no difference so @() = ?() and +() = *()
MWE:
Code:
touch a b aa ab ba
echo @(a|b)
a b
echo ?(a|b)
a b
so searching 0 or 1 (?) occurrences is the same as 1 (@) occurrence.
Code:
echo *(a|b)
a aa ab b ba
echo +(a|b)
a aa ab b ba
again, searching 0 or more (*) is then same as 1 or more (+) occurrences.
again, it is about full match, not a partial match.
with other words: in case of a matching like this: pattern ~ sample a successful match means the pattern will/must cover the whole sample, not only a part of it.
@(a) will not match as, ab, whatever and any other strings, just a and/or empty string.
This engine (extglob) cannot do partial match, just only full match, so if you want to look for "anything else" you need to add it explicitly to the pattern. This a *, so what you are looking for is: @(a)*
Try this (too) and you will see the difference.
why should I care it "searches" also empty strings but returns only hits with non-empty ones?. It seems that searching "0 occurrences" makes no difference so @() = ?() and +() = *()
MWE:
Code:
touch a b aa ab ba
echo @(a|b)
a b
echo ?(a|b)
a b
so searching 0 or 1 (?) occurrences is the same as 1 (@) occurrence.
Code:
echo *(a|b)
a aa ab b ba
echo +(a|b)
a aa ab b ba
again, searching 0 or more (*) is then same as 1 or more (+) occurrences.
The zero-or-more matches would include an empty filename - but this is not allowed.
But the following makes the difference visible:
Code:
shopt -s extglob
for pat in "*(a|b)" "+(a|b)" "?(a|b)" "@(a|b)"
do
for x in "" a b ab bb
do
case $x in
( $pat )
echo "$pat : '$x' matches, case-esac"
;;
esac
if [[ $x == $pat ]]
then
echo "$pat : '$x' matches, [[ == ]]"
fi
done
done
Last edited by MadeInGermany; 11-28-2023 at 05:18 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.