* Possibly wrong handling of $_? @ 2014-12-23 22:34 Vadim Zeitlin 2014-12-23 23:21 ` Harald van Dijk 0 siblings, 1 reply; 5+ messages in thread From: Vadim Zeitlin @ 2014-12-23 22:34 UTC (permalink / raw) To: dash [-- Attachment #1: Type: TEXT/PLAIN, Size: 557 bytes --] Hello, I'm not exactly sure if this is a bug because I didn't find any specification about how is this supposed to behave (to my surprise it turned out that $_ was not in POSIX), but please consider this: % zsh -c 'echo -n foo && echo $_' foofoo % bash -c 'echo -n foo && echo $_' foofoo % dash -c 'echo -n foo && echo $_' foo/usr/bin/dash I've tested several different versions of zsh (4 and 5) and bash (3 and 4) and dash 0.5.5 and 0.5.7 from Debian and they all produced the results as above. Shouldn't dash follow the other shells here? VZ [-- Attachment #2: Type: APPLICATION/PGP-SIGNATURE, Size: 196 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Possibly wrong handling of $_? 2014-12-23 22:34 Possibly wrong handling of $_? Vadim Zeitlin @ 2014-12-23 23:21 ` Harald van Dijk 2014-12-23 23:33 ` Re[2]: " Vadim Zeitlin 0 siblings, 1 reply; 5+ messages in thread From: Harald van Dijk @ 2014-12-23 23:21 UTC (permalink / raw) To: Vadim Zeitlin; +Cc: dash On 23/12/2014 23:34, Vadim Zeitlin wrote: > Hello, > > I'm not exactly sure if this is a bug because I didn't find any > specification about how is this supposed to behave (to my surprise it > turned out that $_ was not in POSIX), but please consider this: > > % zsh -c 'echo -n foo && echo $_' > foofoo > % bash -c 'echo -n foo && echo $_' > foofoo > % dash -c 'echo -n foo && echo $_' > foo/usr/bin/dash This does come across as somewhat confusing, but $_ is really not a special variable at all in dash. The shell you're using to launch dash does make it a special variable. That shell puts _ in the environment, which dash then picks up, and ignores, other than making it available as $_. You can see what your usual shell is doing by testing with other commands: just run env | grep '^_=' and you'll probably see _=/usr/bin/env It works the same way when starting dash. > I've tested several different versions of zsh (4 and 5) and bash (3 and 4) > and dash 0.5.5 and 0.5.7 from Debian and they all produced the results as > above. > > Shouldn't dash follow the other shells here? > VZ If dash did something special with $_, then I agree it would be nice if it would be somewhat compatible with other shells. If dash simply does not implement a feature, that feature is not required by any standard, and that feature is not widely used, then I suspect there won't be a lot of interest in implementing that feature. Don't be put off by that, though. You are free, of course, if you feel so, to attempt to convince people $_ is an important feature that all shells should implement. If you have compelling use cases, if it solves real problems, and if many other shells already implement it, you might even get it standardised. I have never seen a need for it, but that's just me speaking from personal experience, others may feel differently. Cheers, Harald van Dijk ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re[2]: Possibly wrong handling of $_? 2014-12-23 23:21 ` Harald van Dijk @ 2014-12-23 23:33 ` Vadim Zeitlin 2014-12-25 14:32 ` Jilles Tjoelker 0 siblings, 1 reply; 5+ messages in thread From: Vadim Zeitlin @ 2014-12-23 23:33 UTC (permalink / raw) To: dash; +Cc: Harald van Dijk [-- Attachment #1: Type: TEXT/PLAIN, Size: 2684 bytes --] On Wed, 24 Dec 2014 00:21:09 +0100 Harald van Dijk <harald@gigawatt.nl> wrote: HvD> On 23/12/2014 23:34, Vadim Zeitlin wrote: HvD> > Hello, HvD> > HvD> > I'm not exactly sure if this is a bug because I didn't find any HvD> > specification about how is this supposed to behave (to my surprise it HvD> > turned out that $_ was not in POSIX), but please consider this: HvD> > HvD> > % zsh -c 'echo -n foo && echo $_' HvD> > foofoo HvD> > % bash -c 'echo -n foo && echo $_' HvD> > foofoo HvD> > % dash -c 'echo -n foo && echo $_' HvD> > foo/usr/bin/dash HvD> HvD> This does come across as somewhat confusing, but $_ is really not a HvD> special variable at all in dash. Ah, this does explain it, thanks! HvD> If dash did something special with $_, then I agree it would be nice if HvD> it would be somewhat compatible with other shells. If dash simply does HvD> not implement a feature, that feature is not required by any standard, HvD> and that feature is not widely used, then I suspect there won't be a lot HvD> of interest in implementing that feature. Yes, I understand, somehow the idea that dash didn't implement it at all just didn't occur to me, but clearly adding a non-standard new feature is not nowhere near as important as fixing [what looked like] a bug. I'm not sure about the "not widely part", but I don't have any non-anecdotal evidence one way or the other. HvD> Don't be put off by that, though. You are free, of course, if you feel HvD> so, to attempt to convince people $_ is an important feature that all HvD> shells should implement. If you have compelling use cases, if it solves HvD> real problems, and if many other shells already implement it, you might HvD> even get it standardised. I have never seen a need for it, but that's HvD> just me speaking from personal experience, others may feel differently. FWIW my initial problem started with using builddir := $(shell mkdir -p some-long-make-expression && echo $_) in a makefile, which seemed like a nice way to assign the value to the variable only if the directory was successfully created. This can, of course, be done in a myriad of other ways, but this one just seemed like the most elegant to me. Whether this counts as a "need" or not is not for me to say. Personally I'd say the main argument for adding support for "$_" to dash would be to avoid mysterious problems like the one I just had because dash silently (i.e. without giving any errors) behaves differently from the other shells, which in my case resulted in the makefile misbehaving only under Debian (where dash is used as /bin/sh) but not under systems. Anyhow, thanks again for your explanation! VZ [-- Attachment #2: Type: APPLICATION/PGP-SIGNATURE, Size: 196 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Possibly wrong handling of $_? 2014-12-23 23:33 ` Re[2]: " Vadim Zeitlin @ 2014-12-25 14:32 ` Jilles Tjoelker 2014-12-25 22:05 ` Harald van Dijk 0 siblings, 1 reply; 5+ messages in thread From: Jilles Tjoelker @ 2014-12-25 14:32 UTC (permalink / raw) To: Vadim Zeitlin; +Cc: dash, Harald van Dijk On Wed, Dec 24, 2014 at 12:33:32AM +0100, Vadim Zeitlin wrote: > On Wed, 24 Dec 2014 00:21:09 +0100 Harald van Dijk <harald@gigawatt.nl> wrote: > HvD> On 23/12/2014 23:34, Vadim Zeitlin wrote: > HvD> > Hello, > HvD> > > HvD> > I'm not exactly sure if this is a bug because I didn't find any > HvD> > specification about how is this supposed to behave (to my surprise it > HvD> > turned out that $_ was not in POSIX), but please consider this: > HvD> > > HvD> > % zsh -c 'echo -n foo && echo $_' > HvD> > foofoo > HvD> > % bash -c 'echo -n foo && echo $_' > HvD> > foofoo > HvD> > % dash -c 'echo -n foo && echo $_' > HvD> > foo/usr/bin/dash > HvD> > HvD> This does come across as somewhat confusing, but $_ is really not a > HvD> special variable at all in dash. > Ah, this does explain it, thanks! Dash does implement $_, but only in interactive mode. Your selection of shells makes it appear as if dash is the odd one out, but in fact it is not. FreeBSD /bin/sh (also an Almquist shell derivative), mksh and ksh93 also only implement $_ in interactive mode. Details of how $_ differ as well, particularly in ksh93. It seems unwise to use this feature in scripts. > HvD> If dash did something special with $_, then I agree it would be nice if > HvD> it would be somewhat compatible with other shells. If dash simply does > HvD> not implement a feature, that feature is not required by any standard, > HvD> and that feature is not widely used, then I suspect there won't be a lot > HvD> of interest in implementing that feature. > Yes, I understand, somehow the idea that dash didn't implement it at all > just didn't occur to me, but clearly adding a non-standard new feature is > not nowhere near as important as fixing [what looked like] a bug. I'm not > sure about the "not widely part", but I don't have any non-anecdotal > evidence one way or the other. > HvD> Don't be put off by that, though. You are free, of course, if you feel > HvD> so, to attempt to convince people $_ is an important feature that all > HvD> shells should implement. If you have compelling use cases, if it solves > HvD> real problems, and if many other shells already implement it, you might > HvD> even get it standardised. I have never seen a need for it, but that's > HvD> just me speaking from personal experience, others may feel differently. > FWIW my initial problem started with using > builddir := $(shell mkdir -p some-long-make-expression && echo $_) > in a makefile, which seemed like a nice way to assign the value to the > variable only if the directory was successfully created. This can, of > course, be done in a myriad of other ways, but this one just seemed like > the most elegant to me. Whether this counts as a "need" or not is not for > me to say. > Personally I'd say the main argument for adding support for "$_" to dash > would be to avoid mysterious problems like the one I just had because dash > silently (i.e. without giving any errors) behaves differently from the > other shells, which in my case resulted in the makefile misbehaving only > under Debian (where dash is used as /bin/sh) but not under systems. Such a principle of least surprise is not a design goal for dash. -- Jilles Tjoelker ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Possibly wrong handling of $_? 2014-12-25 14:32 ` Jilles Tjoelker @ 2014-12-25 22:05 ` Harald van Dijk 0 siblings, 0 replies; 5+ messages in thread From: Harald van Dijk @ 2014-12-25 22:05 UTC (permalink / raw) To: Jilles Tjoelker, Vadim Zeitlin; +Cc: dash On 25/12/2014 15:32, Jilles Tjoelker wrote: > On Wed, Dec 24, 2014 at 12:33:32AM +0100, Vadim Zeitlin wrote: >> On Wed, 24 Dec 2014 00:21:09 +0100 Harald van Dijk <harald@gigawatt.nl> wrote: >> HvD> On 23/12/2014 23:34, Vadim Zeitlin wrote: >> HvD> > Hello, >> HvD> > >> HvD> > I'm not exactly sure if this is a bug because I didn't find any >> HvD> > specification about how is this supposed to behave (to my surprise it >> HvD> > turned out that $_ was not in POSIX), but please consider this: >> HvD> > >> HvD> > % zsh -c 'echo -n foo && echo $_' >> HvD> > foofoo >> HvD> > % bash -c 'echo -n foo && echo $_' >> HvD> > foofoo >> HvD> > % dash -c 'echo -n foo && echo $_' >> HvD> > foo/usr/bin/dash >> HvD> >> HvD> This does come across as somewhat confusing, but $_ is really not a >> HvD> special variable at all in dash. > >> Ah, this does explain it, thanks! > > Dash does implement $_, but only in interactive mode. Ah, apologies. In interactive mode, it indeed implements it, but differently from bash. In dash, only commands that specify a command name cause $_ to be set. For example, dash -ic 'if a=b; then c=d; else e=f; fi; echo $_' still prints /usr/bin/dash (or wherever dash is installed), because if statements, and variable assignments, have no effect on $_. In bash, this prints a blank line, because the prior value of $_ has not survived those variable assignments. Cheers, Harald van Dijk ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-12-25 22:05 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-12-23 22:34 Possibly wrong handling of $_? Vadim Zeitlin 2014-12-23 23:21 ` Harald van Dijk 2014-12-23 23:33 ` Re[2]: " Vadim Zeitlin 2014-12-25 14:32 ` Jilles Tjoelker 2014-12-25 22:05 ` Harald van Dijk
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).