git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 23, 2024, 00:10 UTC
Message-ID
<CAPig+cQnUBxttpTnucj527T0ExH1fHER4mrbyoU+hEh+Kp+Bow@mail.gmail.com>
In-Reply-To
<xmqqwmld55y1.fsf@gitster.g>
On Mon, Jul 22, 2024 at 8:05 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 17 quoted lines
> Over the years, we accumulated the community wisdom to avoid the
> common "one-short export" construct for shell functions, but seem to
> have lost on which exact platform it is known to fail.  Now during
> an investigation on a breakage for a recent topic, we found one
> example of failing shell.  Let's document that.
> [...]
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines
> @@ -204,6 +204,33 @@ For shell scripts specifically (not exhaustive):
> + - The common construct
> +
> +       VAR=VAL command args
> +
> +   to temporarily set and export environment variable VAR only while
> +   "command args" is running is handy, but this triggers an
> +   unspecified behaviour accoreding to POSIX when used for a command
s/accoreding/according/
Show 19 quoted lines
> +   that is not an external command (like shell functions).  Indeed,
> +   some versions of dash (like 0.5.10.2-6 found on Ubuntu 20.04) and
> +   AT&T ksh do make a temporary assignment without exporting the
> +   variable, in such a case.  Do not use it for shell functions.  A
> +   common workaround is to do an explicit export in a subshell, like
> +   so:
> +
> +       (incorrect)
> +       VAR=VAL func args
> +
> +       (correct)
> +       (
> +               VAR=VAL &&
> +               export VAR &&
> +               func args
> +       )
> +
> +   but be careful that the effect "func" makes to the variables in the
> +   current shell will be lost across the subshell boundary.
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 7 in “CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"”
  1. CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"Junio C Hamano, Jul 22, 2024
  2. CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"Junio C Hamano, Jul 23, 2024
  3. Eric SunshineJul 23, 2024
  4. Junio C HamanoJul 23, 2024
  5. Jeff KingJul 23, 2024
  6. Junio C HamanoJul 23, 2024
  7. Rubén JustoJul 23, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.