Re: variable assignments and parameter expansion in a single command
Kevin F. Quinn wrote: Following a discussion we had earlier this year regarding the order of evaluation of variables and variable assignments: $ A=moo B=$A more env |grep ^B B=moo more (rather than showing just 'B= more') the dash maintainer has highlighted the following: $ bash -c 'K=dvb0.net0 A=${K#dvb} eval echo \$A' This is a bug in bash. There are certain circumstances where the expansion of A will refer to the wrong version of $K. It's state- dependent, so it's been missed so far. I fixed it today. In general, expansions of assignment statements destined for the temporary environment should check for variables in the temporary environment before the normal shell variable table. That preserves the left-to-right processing order. $ bash -c 'a=/bin PATH=$a ls /dev/null' bash: line 1: ls: No such file or directory This is the same problem as the first case. $ bash -c 'x=${K:=dvb0.net0} A=${K#dvb} echo $A' This behaves correctly. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer Live Strong. No day but today. Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/ ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: variable assignments and parameter expansion in a single command
On Sun, 25 Mar 2007 12:26:32 +0200 Kevin F. Quinn [EMAIL PROTECTED] wrote: FWIW I tried also /usr/xpg4/bin/sh (with the Belenix livecd, SunOS 5.11) and that shows the same as sh/ash/dash/bb. FI just tried on SunOS 5.8 (sparc) - a proper Sun installation - and /usr/xpg4/bin/sh there shows one two (i.e behaves the same as bash-3.2, ksh-93) - maybe Belenix has the wrong thing for /usr/xpg4/bin/sh. -- Kevin F. Quinn signature.asc Description: PGP signature ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: variable assignments and parameter expansion in a single command
Mike Frysinger [EMAIL PROTECTED] writes: 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: moo , moo more moo , more Each variable assignment shall be expanded [...] prior to assigning the value. That means that each assignment is supposed to be expanded _and_ performed in the same step while iterating over them. Andreas. -- Andreas Schwab, SuSE Labs, [EMAIL PROTECTED] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 And now for something completely different. ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: variable assignments and parameter expansion in a single command
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. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer Live Strong. No day but today. Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/ ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: variable assignments and parameter expansion in a single command
On Sat, 24 Mar 2007 14:23:39 -0400 Chet Ramey [EMAIL PROTECTED] 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 signature.asc Description: PGP signature ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash