[kwlug-disc] The amazing colon trick in GLOB matching within [[ ]] for bash

D. Hugh Redelmeier hugh at mimosa.com
Thu Mar 12 12:23:31 EDT 2026


> From: Federer Fanatic <nafdef at gmail.com>

I don't understand your posting.  Ignorance on my part.  Time for me to 
learn.

I've been using Bourne Shell since the late 1970s (when it came out).
Most of that time I've actually been using BASH as if it were the Bourne 
Shell (with history) and haven't carefully studied the extensions.

I think that BASH's language is way too complicated and poorly designed.  
But that's mostly OK for me since I use the Bourne Shell subset most of 
the time.

> I found this bit of
> magic in one of them I was not aware of but actually needed (normally one
> just uses an external command in bash, afterall it's a shell:
> 
> if [[ ":${PATH}:" == *":${HOME}/.local/bin:"* ]]; then echo YES; else echo
> NO; fi
> 
> and you do not need to enable globbing via shopt, the glob pattern is not
> =~ construct but a check for equality....amazing ability for bash later
> versions.

It turns out that BASH interprets right side of == within [[ ]] in a 
surprising manner.  I always assume that an equality test is symmetric, 
but not in BASH.  The right side of == is a pattern!  The actual rules 
seem really hacky to me: quotes have a special-to-this-context meaning 
(the manpage doesn't say what kind of quoting).  I always assumed that =~ 
was used whenever you wanted pattern matching.

The other surprise to me is ~/.local/bin.  I must have missed that 
convention's introduction.  I take it that it is for the use of packages 
that are being installed by a user.  Where is this documented?  I have it 
on my $PATH but the directory does not exist (Fedora)..

I assume that "the amazing colon trick" is really just adding : at either 
end of $PATH so that every component begins and ends with :.


More information about the kwlug-disc mailing list