[Top][All Lists]

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

Re: discrepancy with variable assignments and simple commands between sh

From: Jan Schampera
Subject: Re: discrepancy with variable assignments and simple commands between sh and bash
Date: Wed, 25 Aug 2010 07:39:26 +0200
User-agent: Mozilla-Thunderbird (X11/20100329)

Mike Frysinger wrote:
On Wednesday, August 25, 2010 00:59:58 Jan Schampera wrote:
Mike Frysinger wrote:
the difference here being the value in variable "a" after function "f"
finishes executing.  i was expecting the behavior of `bash`, not of `sh`.
 i cant seem to find anything covering this in the man page except for
perhaps interpreting the meaning of some sections to mean this behavior
is allowed. but i certainly didnt locate anything that would imply
behavior of this would differ across bash and sh ...
I'd expect "VAR=VAL <simple command>" to behave like in your "bash"
example, in any case (i.e. also for functions!). Just intuitively, I
mean. I don't know any standard documents about it, maybe it is
"implementation defined", as so often.

Dash behaves the same way as your "sh" example, Korn too. Z behaves like
the "bash" example here. So I fear it actually isn't standardized and
you can't operate with functions the same way you operate with separate

unfortunately, ive relied on this bash behavior in the past in bash-specific scripts because it's an easy way of tweaking a variable's value for a specific command without having to save/restore context. which is why i was surprised when it didnt work in a #!/bin/sh script (which had sh->bash). not that standards bodies care about the pains they inflict upon me ;).

Okay, I think I found something

[POSIX excerpts]
- 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 Special Built-In Utilities . - Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be the case with a regular built-in or other utility.

[Bash docs about POSIX mode]
 24. Assignment statements preceding shell function calls persist in the
     shell environment after the function returns, as if a POSIX
     special builtin command had been executed.

Conclusion (still waiting for the Austin Group undercover guy):
- POSIX specifies this behaviour
- Bash in Bash-mode overrides this (which results in intuitive behaviour, IMHO)
- Bash in POSIX-mode respects it


reply via email to

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