[GTALUG] Bash does-directory-exist question

John Sellens jsellens at syonex.com
Fri Jul 10 17:50:59 EDT 2020


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.

Cheers

John


More information about the talk mailing list