Skip to content

Commit ca9221c

Browse files
committed
Merge branch 'jc/doc-one-shot-export-with-shell-func'
It has been documented that we avoid "VAR=VAL shell_func" and why. * jc/doc-one-shot-export-with-shell-func: CodingGuidelines: document a shell that "fails" "VAR=VAL shell_func"
2 parents 6c70d65 + 728a196 commit ca9221c

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)