zsh-users
 help / color / mirror / code / Atom feed
* Problems with Parameter Expansion :=
@ 2001-02-14 20:28 Heinrich Götzger
  2001-02-14 20:56 ` Bart Schaefer
  2001-02-14 22:11 ` Thomas Köhler
  0 siblings, 2 replies; 3+ messages in thread
From: Heinrich Götzger @ 2001-02-14 20:28 UTC (permalink / raw)
  To: zsh-users

Hi there again,

my second problem is that I want to set NAME to WORD,
if NAME is not set, example from zsh.info:
${NAME:=WORD}

my script (called script.sh):
-- begin --
if [ ${NAME:=`hostname | cut -c5-8`} ]
then
        echo $NAME
fi

if [ ${NAME1:=""} = "true" ]
then
        echo "true"
fi
-- end --
asuming, my hosts name is exploding:

$ echo $NAME
zsh: NAME: parameter not set
$ echo $NAME1
zsh: NAME1: parameter not set
$ . script.sh
odin
script.sh:6: parse error: condition expected: =

Again, it runs with bash and ksh.

Why does it not expand NAME1 to "" ?
running it with set -x it shows:
$ . script.sh
+-zsh:8> . script.sh
+script.sh:1> [ odin ]
+script.sh:3> echo odin
odin
+script.sh:6> [ = true ]
script.sh:6: parse error: condition expected: =

Any ideas?

Thanks for our help.

Regards

Heinrich


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

* Re: Problems with Parameter Expansion :=
  2001-02-14 20:28 Problems with Parameter Expansion := Heinrich Götzger
@ 2001-02-14 20:56 ` Bart Schaefer
  2001-02-14 22:11 ` Thomas Köhler
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2001-02-14 20:56 UTC (permalink / raw)
  To: zsh-users, Heinrich Go%tzger

On Feb 14,  9:28pm, Heinrich Go%tzger wrote:
> Subject: Problems with Parameter Expansion :=
> 
> if [ ${NAME1:=""} = "true" ]
> then
>         echo "true"
> fi

> $ . script.sh
> odin
> script.sh:6: parse error: condition expected: =
> 
> Why does it not expand NAME1 to "" ?

We've actually just been having a discussion about this on zsh-workers.

${NAME1:=""} sets NAME1 to the empty string and then substitutes the
resulting value of $NAME1, which is exactly what the zsh docs say it
will do.

Bourne shell and ksh, however, set NAME1 to the empty string and then
substitute the empty string, which is not the same thing.

The most straightforward thing that will work in all of sh/ksh/zsh is

	if [ "${NAME1:=''}" = "true" ]

so that the entire ${...} is quoted.


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

* Re: Problems with Parameter Expansion :=
  2001-02-14 20:28 Problems with Parameter Expansion := Heinrich Götzger
  2001-02-14 20:56 ` Bart Schaefer
@ 2001-02-14 22:11 ` Thomas Köhler
  1 sibling, 0 replies; 3+ messages in thread
From: Thomas Köhler @ 2001-02-14 22:11 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1904 bytes --]

On Wed, Feb 14, 2001 at 09:28:04PM +0100,
Heinrich Götzger <goetzger@gmx.net> wrote:
> 
> Hi there again,
> 
> my second problem is that I want to set NAME to WORD,
> if NAME is not set, example from zsh.info:
> ${NAME:=WORD}

Fine :-)

> my script (called script.sh):
> -- begin --
> if [ ${NAME:=`hostname | cut -c5-8`} ]
> then
>         echo $NAME
> fi
> 
> if [ ${NAME1:=""} = "true" ]
> then
>         echo "true"
> fi
> -- end --
> asuming, my hosts name is exploding:
> 
> $ echo $NAME
> zsh: NAME: parameter not set
> $ echo $NAME1
> zsh: NAME1: parameter not set
> $ . script.sh
> odin
> script.sh:6: parse error: condition expected: =

Of course :-)

> Again, it runs with bash and ksh.
> 
> Why does it not expand NAME1 to "" ?

It expands NAME1 to an empty string, which I would expect. Why? Let's
see:
if [ ${NAME1:=""} = "true" ] ; then echo "true" ; fi
     ^^^^^^^^^^^^
     Put value of $NAME1 here. Use empty default. Oops, the shell sees
     this:
if [  = "true" ] ; then echo "true" ; fi


> running it with set -x it shows:
> $ . script.sh
> +-zsh:8> . script.sh
> +script.sh:1> [ odin ]
> +script.sh:3> echo odin
> odin
> +script.sh:6> [ = true ]
> script.sh:6: parse error: condition expected: =
> 
> Any ideas?

Of course :-)

Either use [[ ]] like this:
if [[ ${NAME1:=""} = "true" ]] ; then echo "true" ; fi

Or, if your shell is also supposed to run with bash/ksh, use something
like this:
if [ "${NAME1:=''}" = "true" ] ; then echo "true" ; fi

> Thanks for our help.
> 
> Regards
> 
> Heinrich

Ciao,
Thomas

-- 
 Thomas Köhler Email:   jean-luc@picard.franken.de     | LCARS - Linux
     <><        WWW:     http://jeanluc-picard.de      | for Computers
                IRC:             jeanluc               | on All Real
               PGP public key available from Homepage! | Starships

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

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

end of thread, other threads:[~2001-02-14 23:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-14 20:28 Problems with Parameter Expansion := Heinrich Götzger
2001-02-14 20:56 ` Bart Schaefer
2001-02-14 22:11 ` Thomas Köhler

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