Re: sh vs. bash -xc 'a=b c=$a'

2024-05-23 Thread alex xmb sw ratchev
if u
var=value command ;
the var's only for cmd and disappears afterwards

if u
var=val ; cmd ;
its not for cmd , excepts exported
and is set after cmd , usual behav

old rule

On Thu, May 23, 2024, 3:34 PM Robert Elz  wrote:

> Date:Thu, 23 May 2024 09:04:48 -0400
> From:Chet Ramey 
> Message-ID:  
>
>
>   | The bash output clearly tells you the
>   | order of operations, which is the whole purpose of -x.
>
> But it can be horribly misleading.   Consider what bash does with
> this similar case (I used 5.3a1 but I suspect any version would do)
>
> bash-5.3a1 -xc 'a=a b=b; a=1 b=2 cat /dev/null; echo $a'
> + a=a
> + b=b
> + a=1
> + b=2
> + cat /dev/null
> + echo a
> a
>
> versus
>
> bash-5.3a1 -xc 'a=a b=b; a=1 b=2; cat /dev/null; echo $a'
> + a=a
> + b=b
> + a=1
> + b=2
> + cat /dev/null
> + echo 1
> 1
>
> The only difference in the two outputs from -x is the arg to the echo
> command at the end, no indication at all why they're different.
>
>
> On the other hand:
>
> sh -xc 'a=a b=b; a=1 b=2 cat /dev/null; echo $a'
> + a=a b=b
> + a=1 b=2 cat /dev/null
> + echo a
> a
>
> sh -xc 'a=a b=b; a=1 b=2 ; cat /dev/null; echo $a'
> + a=a b=b
> + a=1 b=2
> + cat /dev/null
> + echo 1
> 1
>
> and it is quite clear what is happening.
>
> Personally I'd think it more likely that your average reader would
> interpret "a=1 b=2" as being executed left to right than that they'd
> somehow guess that "a=1" isn't really happening in shell environment.
>
> kre
>
>
>


Re: sh vs. bash -xc 'a=b c=$a'

2024-05-23 Thread Robert Elz
Date:Thu, 23 May 2024 09:04:48 -0400
From:Chet Ramey 
Message-ID:  


  | The bash output clearly tells you the
  | order of operations, which is the whole purpose of -x.

But it can be horribly misleading.   Consider what bash does with
this similar case (I used 5.3a1 but I suspect any version would do)

bash-5.3a1 -xc 'a=a b=b; a=1 b=2 cat /dev/null; echo $a'
+ a=a
+ b=b
+ a=1
+ b=2
+ cat /dev/null
+ echo a
a

versus

bash-5.3a1 -xc 'a=a b=b; a=1 b=2; cat /dev/null; echo $a'
+ a=a
+ b=b
+ a=1
+ b=2
+ cat /dev/null
+ echo 1
1

The only difference in the two outputs from -x is the arg to the echo
command at the end, no indication at all why they're different.


On the other hand:

sh -xc 'a=a b=b; a=1 b=2 cat /dev/null; echo $a'
+ a=a b=b
+ a=1 b=2 cat /dev/null
+ echo a
a

sh -xc 'a=a b=b; a=1 b=2 ; cat /dev/null; echo $a'
+ a=a b=b
+ a=1 b=2
+ cat /dev/null
+ echo 1
1

and it is quite clear what is happening.

Personally I'd think it more likely that your average reader would
interpret "a=1 b=2" as being executed left to right than that they'd
somehow guess that "a=1" isn't really happening in shell environment.

kre




Re: sh vs. bash -xc 'a=b c=$a'

2024-05-23 Thread Chet Ramey

On 5/22/24 10:57 PM, Oğuz wrote:


And some Bourne shells expand command substitutions first

$ (exit 5)
$ x=$? y=`exit 10`
$ echo $x
10


It's worse than that. The v7 sh performed assignments right to left.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



OpenPGP_signature.asc
Description: OpenPGP digital signature


Re: sh vs. bash -xc 'a=b c=$a'

2024-05-23 Thread Chet Ramey

On 5/22/24 6:56 PM, Dan Jacobson wrote:

It seems these should both make one line "+ a=b c=b" output,

for s in sh bash
do $s -xc 'a=b c=$a'
done

I mean they give the same results, but bash splits it into
two lines, so the user reading the bash -x output cannot tell
if one (correct) or two (incorrect) lines were used.


There's nothing incorrect about it. The bash output clearly tells you the
order of operations, which is the whole purpose of -x.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



OpenPGP_signature.asc
Description: OpenPGP digital signature


Re: sh vs. bash -xc 'a=b c=$a'

2024-05-22 Thread Robert Elz
Date:Thu, 23 May 2024 05:57:05 +0300
From:=?UTF-8?B?T8SfdXo=?= 
Message-ID:  


  | On Thu, May 23, 2024 at 2:49 AM Steffen Nurpmeso  wrote:
  | > Only to note that this is not portable.
  | Nor will NetBSD sh.

That's right, and this is expressly unspecified in POSIX.

But I think this aspect is missing the point of the OP's message,
which might have been better illustrated without stepping into
unspecified behaviour using

$SHELL -x -c 'x=1 y=2'

I can see the point, this is two var-assigns on a single (empty)
command, not two commands, which

$SHELL -x -c 'x=1 ; y=2'

would be (and which is fully specified to be each fully executed
before the next is started, avoiding the other issue).

As a comparison consider

$SHELL -x -c 'x=1 y=2 :'

which the first above is almost equivalent to (the difference is
the exit status should there be any command substitutions involved).

However, I don't really see that this should matter all that much.
(-x output is not specified anywhere, just that it happens).

kre



Re: sh vs. bash -xc 'a=b c=$a'

2024-05-22 Thread Oğuz
On Thu, May 23, 2024 at 2:49 AM Steffen Nurpmeso  wrote:
> Only to note that this is not portable.
> The FreeBSD shell will not assign "b" to "c" for this one!

Nor will NetBSD sh. This lets you swap values of two variables without
using a third

$ x=1 y=2
$ x=$y y=$x
$ echo $x $y
2 1

And some Bourne shells expand command substitutions first

$ (exit 5)
$ x=$? y=`exit 10`
$ echo $x
10



Re: sh vs. bash -xc 'a=b c=$a'

2024-05-22 Thread Steffen Nurpmeso
Greg Wooledge wrote in
 :
 |On Thu, May 23, 2024 at 06:56:01AM +0800, Dan Jacobson wrote:
 |> It seems these should both make one line "+ a=b c=b" output,
 |> 
 |> for s in sh bash
 |> do $s -xc 'a=b c=$a'

Only to note that this is not portable.
The FreeBSD shell will not assign "b" to "c" for this one!

 |> done
 |> 
 |> I mean they give the same results, but bash splits it into
 |> two lines, so the user reading the bash -x output cannot tell
 |> if one (correct) or two (incorrect) lines were used.
 |> They can tell with sh -x.
 |
 |Does it actually matter?  What makes bash's output "incorrect", exactly?
 |
 |> By the way, I looked up and down the man page,
 |> and wasn't sure if it says one should expect
 |> $c to end up as c= or c=b in fact!
 |
 |I don't know where it's documented, but assignments and expansions are
 |always performed left to right.  In your example, a value is assigned
 |to variable a before $a is expanded.
 |
 |> And I'm not sure the man page says to expect two lines or one of -x
 |> output either, when using sh vs. bash.
 |
 |I don't see why it matters.  The purpose of the -x output is to show
 |you what the shell is doing, so that you can debug your script.  As
 |long as the output is *clear*, it's doing its job.
 |
 |In bash's case,
 |
 |hobbit:~$ bash -xc 'a=b c=$a'
 |+ a=b
 |+ c=b
 |
 |you can very easily see the order in which the assignments happen, and
 |the values that are assigned.
 --End of 

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: sh vs. bash -xc 'a=b c=$a'

2024-05-22 Thread Greg Wooledge
On Thu, May 23, 2024 at 06:56:01AM +0800, Dan Jacobson wrote:
> It seems these should both make one line "+ a=b c=b" output,
> 
> for s in sh bash
> do $s -xc 'a=b c=$a'
> done
> 
> I mean they give the same results, but bash splits it into
> two lines, so the user reading the bash -x output cannot tell
> if one (correct) or two (incorrect) lines were used.
> They can tell with sh -x.

Does it actually matter?  What makes bash's output "incorrect", exactly?

> By the way, I looked up and down the man page,
> and wasn't sure if it says one should expect
> $c to end up as c= or c=b in fact!

I don't know where it's documented, but assignments and expansions are
always performed left to right.  In your example, a value is assigned
to variable a before $a is expanded.

> And I'm not sure the man page says to expect two lines or one of -x
> output either, when using sh vs. bash.

I don't see why it matters.  The purpose of the -x output is to show
you what the shell is doing, so that you can debug your script.  As
long as the output is *clear*, it's doing its job.

In bash's case,

hobbit:~$ bash -xc 'a=b c=$a'
+ a=b
+ c=b

you can very easily see the order in which the assignments happen, and
the values that are assigned.