From b82f58bfe396b395bce3452bc0ba2f972fb01ab8 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Tue, 9 Aug 2016 10:20:22 -0400 Subject: basic: support default and alternate values for env expansion Sometimes it's useful to provide a default value during an environment expansion, if the environment variable isn't already set. For instance $XDG_DATA_DIRS is suppose to default to: /usr/local/share/:/usr/share/ if it's not yet set. That means callers wishing to augment XDG_DATA_DIRS need to manually add those two values. This commit changes replace_env to support the following shell compatible default value syntax: XDG_DATA_DIRS=/foo:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share} Likewise, it's useful to provide an alternate value during an environment expansion, if the environment variable isn't already set. For instance, $LD_LIBRARY_PATH will inadvertently search the current working directory if it starts or ends with a colon, so the following is usually wrong: LD_LIBRARY_PATH=/foo/lib:${LD_LIBRARY_PATH} To address that, this changes replace_env to support the following shell compatible alternate value syntax: LD_LIBRARY_PATH=/foo/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} [zj: gate the new syntax under REPLACE_ENV_ALLOW_EXTENDED switch, so existing callers are not modified.] --- man/environment.d.xml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'man/environment.d.xml') diff --git a/man/environment.d.xml b/man/environment.d.xml index 4022c25c36..be7758a2f9 100644 --- a/man/environment.d.xml +++ b/man/environment.d.xml @@ -78,8 +78,16 @@ KEY=VALUE environment variable assignments, separated by newlines. The right hand side of these assignments may reference previously defined environment variables, using the ${OTHER_KEY} - and $OTHER_KEY format. No other elements of shell syntax are supported. - + and $OTHER_KEY format. It is also possible to use + + ${FOO:-DEFAULT_VALUE} + to expand in the same way as ${FOO} unless the + expansion would be empty, in which case it expands to DEFAULT_VALUE, + and use + ${FOO:+ALTERNATE_VALUE} + to expand to ALTERNATE_VALUE as long as + ${FOO} would have expanded to a non-empty value. + No other elements of shell syntax are supported. EachKEY must be a valid variable name. Empty lines and lines beginning with the comment character # are ignored. @@ -95,8 +103,8 @@ FOO_DEBUG=force-software-gl,log-verbose PATH=/opt/foo/bin:$PATH - LD_LIBRARY_PATH=/opt/foo/lib - XDG_DATA_DIRS=/opt/foo/share:${XDG_DATA_DIRS} + LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}/opt/foo/lib + XDG_DATA_DIRS=/opt/foo/share:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/} -- cgit v1.2.1