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

Robert Elz <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
    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?

kre
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.