Timeline for answer to POSIX sh alternative for bash's regexp matching by cas
Current License: CC BY-SA 4.0
Post Revisions
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 25, 2025 at 14:48 | comment | added | Stéphane Chazelas | @CharlesDuffy That behaviour of bash 3.2 (changed from 3.1) whereby quoting affects regex operators is really one they didn't think through as regexp syntax is not compatible with shell tokenising, and that means it's going to be difficult for bash to add support for better (and more de-facto standard these days) regexp syntax such as PCRE. Also prevents it using non-POSIX extensions unless you use a temporary variable. There are a few Q&As convering it here. zsh doesn't have that "feature"; it's very buggy in ksh93. | |
| Nov 25, 2025 at 14:34 | comment | added | Charles Duffy |
@StéphaneChazelas, ...I don't see how [ could follow the =~ behavior of having behavior that varies based on how individual characters are quoted (treating quoted characters as escaped for literal matches only) while retaining parity between builtin and external implementations. Am I missing something?
|
|
| Nov 25, 2025 at 8:56 | history | edited | Toby Speight | CC BY-SA 4.0 |
List item continuation paragraphs
|
| Nov 25, 2025 at 6:15 | comment | added | cas |
You don't need to use && there. Theexit shouldn't depend on the cat or printf succeeding, it should exit unconditionally, so just put exit 3 on a line after the heredoc. And, like I said in my answer to your previous question, you should use a function to print an error message and exit (e.g. the error_exit function I provided as an example). While no code is truly self-documenting, using well-named functions for common tasks can make it a lot easier to read and understand
|
|
| Nov 25, 2025 at 6:00 | vote | accept | EmberNeurosis | ||
| Nov 25, 2025 at 6:00 | comment | added | EmberNeurosis | Many thanks for this lengthy solution! Really cleaned up my scripts now and set some new habits. One question: How do I chain a '&& exit ' after a heredoc? | |
| Nov 24, 2025 at 7:41 | history | edited | cas | CC BY-SA 4.0 |
added 823 characters in body
|
| Nov 24, 2025 at 7:24 | history | edited | cas | CC BY-SA 4.0 |
added 405 characters in body
|
| Nov 24, 2025 at 7:20 | comment | added | Stéphane Chazelas |
I was only making the point that the argument about using awk in a look could also apply to printf, cat or [, not implying that you wrote anything wrong.
|
|
| Nov 24, 2025 at 7:18 | comment | added | Stéphane Chazelas |
perl -C -p -e 'tr/[:upper:]/[:lower:]/' doesn't work in perl. You'd use perl -C -pe '$_ = lc' instead there. I was making that point because I was finding those [upper:] and [:lower:] classes work with at least GNU and BSD tr, maybe others too. tr A-Z a-z works for ASCII misleading into thinking you could use tr '[:upper:]' '[:lower:]' on non-ASCII input with GNU tr.
|
|
| Nov 24, 2025 at 7:09 | comment | added | cas |
If utf-8 is important, use something like perl -C -p -e 'tr/[:upper:]/[:lower:]/' or anything else that can handle multi-byte characters. So far, the OP's code has only shown a need for handling plain ascii options.
|
|
| Nov 24, 2025 at 7:06 | comment | added | cas |
I never claimed or even suggested that cat was built-in. The only time I mentioned built-in was regarding [[ ... ]] - that is built-in to ksh, bash, zsh, etc and has a clear performance advantage over awk if used repeatedly in a loop. For a one-off execution, it wouldn't matter at all.
|
|
| Nov 24, 2025 at 7:04 | comment | added | Stéphane Chazelas |
tr '[:upper:]' '[:lower:]' in current versions of GNU tr only works for single byte characters (so in UTF-8 locales only on on ASCII letters).
|
|
| Nov 24, 2025 at 7:04 | comment | added | cas | yeah, but the OP wanted something that works in posix sh, not non-standard extensions that only work in specific shells. | |
| Nov 24, 2025 at 7:04 | comment | added | Stéphane Chazelas |
Note POSIX doesn't guarantee [ or printf or cat be builtin. In practice, I don't know of any modern shell where [ is not builtin. There aren't many shells where cat is builtin. printf is not builtin in ksh88 or more pdksh derivatives.
|
|
| Nov 24, 2025 at 6:58 | comment | added | Stéphane Chazelas |
There's nothing stopping [ doing regexp matching; the one in zsh or yash do have a =~ operator. ksh's [[...]] and ((...)) were more about having micro-languages easier to use to perform tests (including arithmetic ones with ((...)) with a C-like language) to give feature parity with csh (which was the popular shell at the time).
|
|
| Nov 24, 2025 at 6:53 | history | edited | cas | CC BY-SA 4.0 |
added 45 characters in body
|
| Nov 24, 2025 at 6:45 | history | answered | cas | CC BY-SA 4.0 |