On Fri, Sep 06, 2019 at 08:55:10PM +0200, Andreas Kusalananda Kähäri wrote: > On Fri, Sep 06, 2019 at 11:39:06PM +0500, JohnS wrote: > > Hi, all! > > > > Why next construction doesn't work? > > > > read x; while [ "$x" != [abc] ]; do echo "Not a, b or c"; break; done > > > > I tried many variants but can't make it work. Moreover I don't understand > > WHY it > > doesn't work?! > > > > Thanks! > > The shells in the OpenBSD base system do not support matching regular > expressions with that syntax. You may have been thinking of bash, > > while [[ ! $x =~ [abc] ]]; do ...; done > > Or, in ksh, > > while [[ $x != [abc] ]]; do ...; done > > Here however, [abc] is not a regular expression, but a filename globbing > pattern, and the test will test for equality rather than trying to match > a substring (as would be the case with a regular expression match).
"equality" was not quite what I meant. It would test for a match against the whole of $x, as any globbing pattern would do, rather than a substring of $x, as a regular expression would do. I.e., the bash code above tests whether $x *contains* a, b, or c, while the ksh code beneath it tests whether $x *is* a, b or c (and then the result of the test is negated). > > Note that this test requries [[ ... ]] rather than [ ... ]. See the > ksh(1) manual.