Getting field name and field value with parameter expansion
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.
Getting field name and field value with parameter expansion
I have a bash variable named flentry that consists of
Code:
File: Value
or
Code:
+ Value
Have used
Code:
fld="${flentry%[[:blank:]]*}"
and
Code:
fl="${flentry#*[[:blank:]]}"
to get the field name File: and +, whereas fl gets the value.
Looks as if bash parameter expansion uses regex patterns.
I understand that % remove shortest trailing match, whilst # removes smallest trailing match. But I am having difficulty seeing how fld and fl get the field name and field value respectively.
How exactly are fld and fl being made? Whilst * means zero or more matches, I fail to see what "${flentry#*[[:blank:]]}" tries to match because the * immediately follows #.
The answers to everything you've asked are in this thread are provided in the Bash Reference Manual.
You really should be consulting that first, and opening threads if you can't find the right section and/or don't understand the explanation (in which case provide a direct link and/or quote and explain where you're stuck).
I thought the delete uses a regex for matching, so that the * would mean zero or more characters.
Do not assume anything, that will mislead only you.
Always try your assumption.
Quote:
Originally Posted by Faki
What does the * match exactly. Is it a wildcard representing any number of characters?
Just a comment:
* has a lot of different meaning depending on the tool you use (and also we have other special chars, like these: .?[]+\ just to list a few).
It is usually some kind of wildcard or a non-regular char.
We have different regex implementations, and also different kind of pattern matching engines, so you always have to check which one is actually involved.
Even bash has several different engines, like globs, pattern matching and regex.
So if unsure just read the man page and try it, but do not assume anything.
This is the shell's native pattern matching or "globbing". A * matches zero or more characters, a ? matches one character, a [ ] matches one character out of the character set.
Regular expression (RE) has some similarities but also some fundamental differences.
There are two types of RE, BRE and ERE (and PCRE is an extension to ERE).
Standard shells do not directly support RE; you need helper programs like grep, sed, awk.
The bash shell directly uses an RE in
There are two types of RE, BRE and ERE (and PCRE is an extension to ERE).
That's not great wording, since there are different types of regex engine (not just different engines, but different approaches to how they're implemented), but that is not what you're referring to.
POSIX defines BRE and ERE, but GNU tools have their own extended version of BRE and ERE which have more functionality than POSIX. (I think GNU actually uses the same engine for both BRE/ERE, just with different syntax rules.)
PCRE is not an extension to ERE, it's an unrelated library aiming for - but not achieving - compatibility with Perl (another engine).
Perhaps you were thinking of Tcl, which has ARE; described as "basically EREs with some significant extensions".
Assuming we're just considering shell scripts (not all programming languages), well there's still Python which brings us to SEVEN different regex engines that might reasonably be encountered on a Linux system.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.