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

Re: What's the 'word' in parameter relpacement expansion?



On Fri, 4 Nov 2016 17:00:09 +0800
Han Pingtian <hanpt@xxxxxxxxxxxxxxxxxx> wrote:
> I think
> 
> $ a='abc def'
> $ print ${a:/def}
> 
> will get 'abc ', but it is 'abc def'. So it works just like ${a//#%def}?
> The 'entire word' in the document is the same thing of 'entire string'?

"Word" isn't a very good word, but I'm not sure what would be better.

In this context, what it means is either a complete scalar value,
or an element of an array.  There is an option to use space-delimited
words in substitution, but it's not the default (and I don't think it's
all that commonly used as array processing tends to be more useful).

Hence

% a=(abc def)
% print ${a:/def}
abc
% a=(abc "abc def")
% print ${a:/def}
abc abc def

More widely in the documentation, it's treated as synonymous with a
single command line argument.  That's what it means in "history word",
and I think that's what's inspired the usage here, although it's
confused by the many ways of getting words explicitly or implicitly
split.

If we can find something unambiguous we can change it generally, though
it'll need a lot of editing.

The best we've got at the moment is this in paramater substitution
documentation:


If var(name) is an array parameter, and the tt(KSH_ARRAYS) option is not
set, then the value of each
element of var(name) is substituted, one element per word.  Otherwise, the
expansion results in one word only; with tt(KSH_ARRAYS), this is the first
element of an array.  No field splitting is done on the result unless the
tt(SH_WORD_SPLIT) option is set.
See also the flags tt(=) and tt(s:)var(string)tt(:).


We could add something like the following, though it slightly overlaps
with what I've just quoted.

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index b731516..f465b2f 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -572,6 +572,18 @@ noderef(Modifiers) in noderef(History Expansion) can be
 applied:  for example, tt(${i:s/foo/bar/}) performs string
 substitution on the expansion of parameter tt($i).
 
+In the following descriptions, `word' refers to a single word
+substituted on the command line, not necessarily a space delimited word.
+With default options, after the assignments:
+
+example(array=("first word" "second word")
+scalar="only word")
+
+then tt($array) substitutes two words, `tt(first word)' and `tt(second
+word)', and tt($scalar) substitutes a single word `tt(only word)'.  This
+may be modified by explicit or implicit word-splitting, however.  The
+full rules are complicated and are noted at the end.
+
 startitem()
 item(tt(${)var(name)tt(}))(
 The value, if any, of the parameter var(name) is substituted.



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