bug-bash
[Top][All Lists]
Advanced

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

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


From: Steffen Nurpmeso
Subject: Re: sh vs. bash -xc 'a=b c=$a'
Date: Thu, 23 May 2024 01:49:12 +0200
User-agent: s-nail v14.9.24-621-g0d1e55f367

Greg Wooledge wrote in
 <Zk57gSGiubfkwN0L@wooledge.org>:
 |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 <Zk57gSGiubfkwN0L@wooledge.org>

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



reply via email to

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