bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: variable assignments and parameter expansion in a single command


From: Kevin F. Quinn
Subject: Re: variable assignments and parameter expansion in a single command
Date: Sat, 24 Mar 2007 22:37:18 +0100

On Sat, 24 Mar 2007 14:23:39 -0400
Chet Ramey <chet.ramey@case.edu> wrote:

> Mike Frysinger wrote:
> > i'm trying to determine whether POSIX allows for utilizing of
> > variables in simple commands that were defined earlier in the same
> > command ... in other words, whether this snippet:
> > unset A B
> > A="moo" B="$A more"
> > echo $A , $B
> > should display moo twice or just once:
> 
> As I read Posix, twice, as long as there are only assignment
> statements in the command.  Assignment statements preceding simple
> commands are treated differently.

I don't see that posix defines simple commands like that - an
assignment _is_ a simple command:

`A "simple command" is a sequence of optional variable assignments and
redirections, in any sequence, optionally followed by words and
redirections, terminated by a control operator.'

which implies to me that:

A=one B="$A two"

is a simple command.

The same section (that Mike referred to -
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01)
also explicitly says that variable assignments are "saved for
processing in steps 3 and 4".  Step 2 expands all words except variable
assignments and redirections - my interpretation of "variable
assignment" is the `A=' part of the statement; i.e. that the rhs of
the assignment is expanded.  Step 3 does the redirections and step 4
does the variable assignments.  So if expansions are done before the
assignments, any assignment referring to other variables that are
assigned, should be expanded with the value before the assignment.

Either way, I've yet to find a shell that claims posix-compliance that
does it the bash way; all of Solaris sh, BSD sh/ash/dash (not sure if
Solaris is BSD or SYSV - or indeed if there's any difference) and
busybox all yield "moo , more" in Mike's example.

Hmm:

$ A=one B="$A two" env
A=one
B= two
...
$ A=one B="$A two" env | grep ^[AB]=
A=one
B=one two

$ cat show_ab
#!/bin/bash
echo A: $A
echo B: $B
$ A=one B="$A two" ./show_ab
A: one
B: two
$ A=one B="$A two" ./show_ab | more
A: one
B: one two

is a little bizarre.  Again; the other shells never yield "one two".

-- 
Kevin F. Quinn

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]