zsh-workers
 help / color / mirror / code / Atom feed
* $var not expanded in ${x?$var}
@ 2023-01-13  8:02 Stephane Chazelas
  2023-01-16  8:35 ` Daniel Shahaf
  2023-01-16 17:15 ` Peter Stephenson
  0 siblings, 2 replies; 3+ messages in thread
From: Stephane Chazelas @ 2023-01-13  8:02 UTC (permalink / raw)
  To: Zsh hackers list

$ zsh -c 'echo ${1?$USERNAME}'
zsh:1: 1: $USERNAME

No quote removal either:

$ zsh -c 'echo ${1?"x"}'
zsh:1: 1: "x"

Doc says:

> In any of the above expressions that test a variable and substitute an
> alternate WORD, note that you can use standard shell quoting in the WORD
> value to selectively override the splitting done by the SH_WORD_SPLIT
> option and the = flag, but not splitting by the s:STRING: flag.

POSIX:

> ${parameter:?[word]}
> Indicate Error if Null or Unset. If parameter is unset or
> null, the *expansion* of word (or a message indicating it is
> unset if word is omitted) shall be written to standard error
> and the shell exits with a non-zero exit status. Otherwise,
> the value of parameter shall be substituted. An interactive
> shell need not exit.

(also note that line number being printed twice)

-- 
Stephane


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: $var not expanded in ${x?$var}
  2023-01-13  8:02 $var not expanded in ${x?$var} Stephane Chazelas
@ 2023-01-16  8:35 ` Daniel Shahaf
  2023-01-16 17:15 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2023-01-16  8:35 UTC (permalink / raw)
  To: Stephane Chazelas, Zsh hackers list

Stephane Chazelas wrote on Fri, 13 Jan 2023 08:02 +00:00:
> $ zsh -c 'echo ${1?$USERNAME}'
> zsh:1: 1: $USERNAME
>
> No quote removal either:
>
> $ zsh -c 'echo ${1?"x"}'
> zsh:1: 1: "x"
>

Thanks for the report.

> (also note that line number being printed twice)

No, it's not.  The second "1" is the parameter name:

$ zsh -fc 'echo ${foo?$USERNAME}' 
zsh:1: foo: $USERNAME

Cheers,

Daniel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: $var not expanded in ${x?$var}
  2023-01-13  8:02 $var not expanded in ${x?$var} Stephane Chazelas
  2023-01-16  8:35 ` Daniel Shahaf
@ 2023-01-16 17:15 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2023-01-16 17:15 UTC (permalink / raw)
  To: zsh workers

> On 13/01/2023 08:02 Stephane Chazelas <stephane@chazelas.org> wrote:
> 
>  
> $ zsh -c 'echo ${1?$USERNAME}'
> zsh:1: 1: $USERNAME
> 
> No quote removal either:
> 
> $ zsh -c 'echo ${1?"x"}'
> zsh:1: 1: "x"
> 
> Doc says:
> 
> > In any of the above expressions that test a variable and substitute an
> > alternate WORD, note that you can use standard shell quoting in the WORD
> > value to selectively override the splitting done by the SH_WORD_SPLIT
> > option and the = flag, but not splitting by the s:STRING: flag.

In fact the shell does not "substitute an alternate WORD" here, it
just prints it out, but the difference is easy to miss and expanding it
seems the right thing to do from other points of view, so I've noted it
in the doc.

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index eb8cdbae5..857715a95 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -665,7 +665,9 @@ item(tt(${)var(name)tt(:?)var(word)tt(}))(
 In the first form, if var(name) is set, or in the second form if var(name)
 is both set and non-null, then substitute its value; otherwise, print
 var(word) and exit from the shell.  Interactive shells instead return to
-the prompt.  If var(word) is omitted, then a standard message is printed.
+the prompt.  If var(word) is omitted, then a standard message is
+printed.  Note that var(word) is expanded even though its value
+is not substituted onto the command line.
 )
 enditem()
 
diff --git a/Src/subst.c b/Src/subst.c
index 897188862..4ad9fee1a 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -3076,7 +3076,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 	    if (vunset) {
                 if (isset(EXECOPT)) {
                     *idend = '\0';
-                    zerr("%s: %s", idbeg, *s ? s : "parameter not set");
+		    if (*s){
+			singsub(&s);
+			zerr("%s: %s", idbeg, s);
+		    } else
+			zerr("%s: %s", idbeg, "parameter not set");
                     /*
                      * In interactive shell we need to return to
                      * top-level prompt --- don't clear this error
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 6bf55b4db..7dd5d82d7 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -110,6 +110,11 @@
 *>*foo:1: 1: no arguments given
 >reached
 
+  message="expand me and remove quotes"
+  (: ${UNSET_PARAM?$message})
+1:${...?....} performs expansion on the message
+?(eval):2: UNSET_PARAM: expand me and remove quotes
+
   print ${set1:+word1} ${set1+word2} ${null1:+word3} ${null1+word4}
   print ${unset1:+word5} ${unset1+word6}
 0:${...:+...}, ${...+...}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-01-16 17:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13  8:02 $var not expanded in ${x?$var} Stephane Chazelas
2023-01-16  8:35 ` Daniel Shahaf
2023-01-16 17:15 ` Peter Stephenson

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