From: Rudolf Polzer <divverent@xonotic.org>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: dash@vger.kernel.org
Subject: Re: Bug: temporary assignments vs shell function
Date: Thu, 14 Jul 2011 12:09:15 +0200 [thread overview]
Message-ID: <20110714100915.GA18946@div0.qc.to> (raw)
In-Reply-To: <20110714092615.GA15476@elie>
On Thu, Jul 14, 2011 at 04:26:16AM -0500, Jonathan Nieder wrote:
> Hi,
>
> Rudolf Polzer wrote:
>
> > foo=bar func
> > echo "foo is now $foo"
> >
> > will export foo=bar in global scope (i.e. it affects the execution environment
> > after the function call).
> [...]
> > A shell function however isn't a "special built-in" and also counts as a
> > "command name", thus the last echo line really should output "foo is now foo",
> > which it does in e.g. bash and the FreeBSD /bin/sh.
>
> From XCU 2.9.5:
>
> | When a function is executed, it shall have the syntax-error and
> | variable-assignment properties described for special built-in
> | utilities in the enumerated list at the beginning of Section 2.14.
Interesting, and well hidden. So this weird behaviour isn't a bug, but an
actual feature. Or, they documented a bug of an early shell implementation and
made it into the spec ;)
> This seems to be one of those odd cases where "bash -o posix" behaves
> differently from bash. Based on this test:
>
> $sh -c 'func () { :; }; foo=bar func; echo $foo'
>
> dash, ksh93, bash -o posix, pdksh and its derivatives, zsh with
> "emulate sh", and busybox sh implement the specified behavior. I'm
> surprised to hear your copy of FreeBSD sh behaves differently.
I just retested with that very command, and yes, the FreeBSD /bin/sh does
behave the "non-POSIX" (and "obvious") way, and so does the Solaris /bin/sh.
So basically - when using an assignment for the duration of a shell command
execution, an explicit subshell must be used in portable scripts, as
interpretation of this construct differs among shells and thus cannot be relied
on. Also, I find this (although standardized) behavior entirely useless, never
"what you want", and in case you REALLY want it, adding a semicolon and
"export" fixes it.
I find it especially vile that this is a hidden way to export a variable... nice
for "obfuscated" shell scripts :P
exported() { :; }
foo=bar exported
sh -c 'echo $foo'
But, interesting that this weird and broken behaviour is standardized, and this
clearly means that dash does the right thing - also, having a difference
against bash here is good as it helps avoid this broken-in-the-spec construct.
Best regards,
Rudolf Polzer
next prev parent reply other threads:[~2011-07-14 10:09 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-14 8:34 Bug: temporary assignments vs shell function Rudolf Polzer
2011-07-14 9:26 ` Jonathan Nieder
2011-07-14 10:09 ` Rudolf Polzer [this message]
2011-07-14 10:25 ` Jonathan Nieder
2011-07-31 22:19 ` Jilles Tjoelker
2011-08-01 5:59 ` Rudolf Polzer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110714100915.GA18946@div0.qc.to \
--to=divverent@xonotic.org \
--cc=dash@vger.kernel.org \
--cc=jrnieder@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox