Skip to content

Commit 728a196

Browse files
committed
CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"
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. This does *not* mean that we can freely start using the construct once Ubuntu 20.04 is retired. But it does mean that we cannot use the construct until Ubuntu 20.04 is fully retired from the machines that matter. Moreover, posix explicitly says that the behaviour for the construct is unspecified. Helped-by: Kyle Lippincott <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2b3f2b commit 728a196

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Documentation/CodingGuidelines

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ For shell scripts specifically (not exhaustive):
204204
local variable="$value"
205205
local variable="$(command args)"
206206

207+
- The common construct
208+
209+
VAR=VAL command args
210+
211+
to temporarily set and export environment variable VAR only while
212+
"command args" is running is handy, but this triggers an
213+
unspecified behaviour according to POSIX when used for a command
214+
that is not an external command (like shell functions). Indeed,
215+
dash 0.5.10.2-6 on Ubuntu 20.04, /bin/sh on FreeBSD 13, and AT&T
216+
ksh all make a temporary assignment without exporting the variable,
217+
in such a case. As it does not work portably across shells, do not
218+
use this syntax for shell functions. A common workaround is to do
219+
an explicit export in a subshell, like so:
220+
221+
(incorrect)
222+
VAR=VAL func args
223+
224+
(correct)
225+
(
226+
VAR=VAL &&
227+
export VAR &&
228+
func args
229+
)
230+
231+
but be careful that the effect "func" makes to the variables in the
232+
current shell will be lost across the subshell boundary.
233+
207234
- Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.
208235
"\xc2\xa2") in printf format strings, since hexadecimal escape
209236
sequences are not portable.

0 commit comments

Comments
 (0)