[GTALUG] Bash does-directory-exist question

Giles Orr gilesorr at gmail.com
Sun Jul 12 21:48:22 EDT 2020


On Fri, 10 Jul 2020 at 17:51, John Sellens <jsellens at syonex.com> wrote:
>
> On Fri, 2020/07/10 05:39:59PM -0400, Giles Orr <gilesorr at gmail.com> wrote:
> | I love this list!  I thought that '[ -w . ]' and '[ -w $PWD ]' were
> | practically equivalent.  "Practically" means, in this case, "almost."
> | But not quite - and the difference is the solution to the problem.
>
> It's a very important, though sometimes subtle, concept in unix-land
> that there are multiple names for just about anything.
>
> Here, obviously, $PWD is a variable substitution equivalent to /some/path,
> which likely existed at some point, but may or may not exist now.  The
> directory "." always (I think) exists, because a process always has a
> current directory open. (Hmmm, but opendir(".") might not work?)
>
> The other canonical example is "how do I remove a file that starts with -?".
> The key to that of course is the multiple names thing "-file" (which looks
> like an option string) is the same as "./-file" (which doesn't).
>
> Once you understand that, the world opens up :-)
>
> Of course, most times "rm -- -file" works but I'm old enough (uh, I mean
> I've read about the history of unix) to know that -- didn't always exist.

Here's a simple implementation of a Bash prompt using what we were discussing:

PS1="\$(if ! [ -w "\${PWD}" ]; then echo -en '\[\033[41m\]' ; fi ;
echo '\w\[\033[0m\]\$ ')"

-- 
Giles
https://www.gilesorr.com/
gilesorr at gmail.com


More information about the talk mailing list