Re: PATH vs current dir and dir sure to be not existing

[email protected]
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
On Sat, Sep 28, 2024 at 06:24:58PM +0700, Robert Elz wrote:
>     Date:        Sat, 28 Sep 2024 10:33:55 +0200
>     From:        <[email protected]>
>     Message-ID:  <[email protected]>
> 
>   | This does mean that one user setting a PATH with variables:
>   |
>   | PATH="/bin:/usr/bin:$MYSCRIPTS:/usr/pkg/bin"
>   |
>   | if MYSCRIPTS is not defined, will end up including the current working
>   | dir in his PATH, while it was not the intention.
> 
> Yes.   The expansion happens before the value is assigned to
> the variable.   There is no difference between that and explicitly
> including ::
> 
>   | The general rule of safety is doing:
>   | PATH="/bin:/usr/bin:${MYSCRIPTS:-/not/existing}:/usr/pkg/bin"
> 
> You could, but that would be wasteful.
> 
> Better would be:
> 
>   PATH="/bin:/usr/bin:${MYSCRIPTS:+${MYSCRIPTS}:}/usr/pkg/bin"
> or
>   PATH="/bin:/usr/bin${MYSCRIPTS:+:${MYSCRIPTS}}:/usr/pkg/bin"
> 
> whichever you prefer (they are essentially the same thing).
> 
> Why stick an entry in PATH that you intend not to be used?

Since I'm writing a pathsearch.7 to explain the PATH_SEARCH_OPT
option(s) for "qualified filenames" (searching in PATH for "dk/ctl", 
"vect/in/dxf" etc.), Il will add your suggestion of expanding with a
colon only if defined. Neat. 

Best,
-- 
        Thierry Laronde <tlaronde +AT+ kergis +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.