TL,DR: Why does my shell script choke on whitespace or other special characters?. Oops, that's even longer… Well, real TL,DR: always put double quotes around variable and command substitutions. Plus see the end of this answer for a second issue with your script.
It's impossible with your script to make a variable expansion expand to an empty string. An unquoted variable expansion undergoes the “expand+split+glob” process:
- Take the value of the variable, which is a string.
- Split the string at whitespace (more generally, at characters that are present in the value of the
IFS
variable). This results in a list of strings (possibly empty if the original string contained nothing but whitespace).
- For each element of the list, if it contains at least one wildcard character
*?\[
and the element is a pattern that matches at least one file, then the element is replaced by the list of matches.
It's possible for $DOMAIN_SUFFIX
to expand to an empty list of strings, but not to a list containing an empty string. The wildcard matching step cannot produce empty elements since a file name is never the empty string. The splitting step cannot produce empty elements with the default value of IFS
, since the separator is a sequence of whitespace and it takes at least one non-whitespace character to interrupt a sequence of whitespace. The splitting step can produce empty elements if IFS
is changed to include non-whitespace characters, but that would require changing the script to modify IFS
and there's no reason to do so.
You need to change the script and make it use a straightforward variable expansion instead of using the expand+split+glob operator: put double quotes around the variable expansion.
Additionally, ${DOMAIN_SUFFIX:=.example.com}
expands to .example.com
if the value of DOMAIN_SUFFIX
is empty. So you'll never get an empty string for that. To preserve the empty value and only use .example.com
if DOMAIN_SUFFIX
is unset, change the :=
to =
.
#!/bin/sh
./somecommand -s "${DOMAIN_SUFFIX=.example.com}"