Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Solved, but now a new twist (was: Double Evaluation Question (not in FAQ))



On Sep 4, 10:43am, Com MN PG P E B Consultant 3 wrote:
}
} That is, if I have an assignment
} 
}   x='~linus    ~steve'
} 
} Is there also a simple solution, which expands $x to the home
} directories of linus and steve, with spacing preserved?

No.  Tildes are only expanded at the beginnings of words, so you
have to split the string (thus discarding the spacing, if you don't
take special care) before the home directories can be substituted.

There's a complicated solution:

setopt extendedglob
print -r ${(j//)${(s/|/)~${x/(#b)( ##)/|$match|}}}

The /(#b)( ##)/ matches a string of one or more spaces, which is
then replaced with itself ($match) surrounded by "|" (anything not
found in the strings on either side would work).  That's then split
(s/|/) on vertical bars to make an array of three words, which are
made eligible for expansion by the tilde that appears between the
closing paren and the start of the inner expansion.  Finally this is
rejoined into a single string (j//) without adding any additional
spaces between the array elements.

I'm actually a little surprised that this works -- I would have
guessed that joining with (j//) took place before tilde expansion.
But it seems to work as far back as backreferences (#b) do, so I
guess it's safe to assume it'll keep working.  (PWS, does something
need to be added to the "Rules" section under "Parameter Expansion"?)



Messages sorted by: Reverse Date, Date, Thread, Author