zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: "Larry P . Schrof" <schrof@cig.mot.com>, zsh-users@sunsite.auc.dk
Subject: Re: Setting paths with ~'s in values.
Date: Fri, 12 Feb 1999 14:05:58 -0800	[thread overview]
Message-ID: <990212140558.ZM11520@candle.brasslantern.com> (raw)
In-Reply-To: <199902122018.PAA29284@po_box.cig.mot.com>

On Feb 12,  2:17pm, Larry P . Schrof wrote:
> Subject: Setting paths with ~'s in values.
> ---
> # Generate a nice-looking path, including only directories that exist.
> for t_path in "${(f)$(<${HOME}/.zpaths)}"
> do
>     if 
>         [[ -d $t_path ]]
>     then 
>         PATH=${PATH}:${~t_path}
>     fi
> done
> 
> PATH=${PATH[2,-1]}	# Nuke the leading ':'
> ---
> 
> From the man page, I was under the impression that putting the ~
> before the variable would expand the '~' in each of those paths.

No.  What the ~ in ${~t_path} does is make the filename *generation*
characters in the value "available" to be processed later, when regular
filename generation takes place (see the "Expansion" part of the info
docs, or "man zshexpn").  This doesn't apply to filename *expansion*,
which is the ~ character.  Even if it did, expansion/generation isn't
done on the right-hand-side of scalar parameter assignments.

The preferred solution to this problem is to use the $path array, which
is automatically converted by zsh to a colon-separated string which is
stuffed into the $PATH environment.  Filename generation and expansion
is done in array assignments -- but that still doesn't expand the ~ .
So you need an additional "eval" for that, which means you don't need
the ~ any more.

Also, you don't need to split the .zpaths file by lines with ${(f)...}
unless some of your paths contain spaces (in which case the eval gets
more complicated too).

A direct translation would thus be:

  for t_path in $(<${HOME}/.zpaths)
  do
      if 
          [[ -d $t_path ]]
      then 
          eval path=\( $path $t_path \)
      fi
  done

and you don't need the extra step of removing the leading colon.  But
there's a better, faster, smarter way to do the same thing:

  eval path=\( ${^$(<${HOME}/.zpaths)}'(|)(/)' \)

Here, the ${^...} takes the place of the "for" loop, the (/) tests a
glob pattern to see if it refers to a directory, and the (|) turns each
of the lines from .zpaths into an equivalent glob pattern so that (/)
test is applied.  The quotes and backslashes just make sure that the
globbing and array assignment steps are delayed until the "eval" runs.


  reply	other threads:[~1999-02-12 22:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-12 20:17 Larry P . Schrof
1999-02-12 22:05 ` Bart Schaefer [this message]
1999-02-12 22:29   ` Bart Schaefer
1999-02-22 22:20   ` Danny Dulai
1999-02-23  4:58     ` Bart Schaefer
1999-02-23  8:39       ` Danny Dulai
1999-02-24  5:07         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=990212140558.ZM11520@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=schrof@cig.mot.com \
    --cc=zsh-users@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).