Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on What guarantees does Bash make about order of :- Parameter Expansion when it is not in POSIX mode?
Post
What guarantees does Bash make about order of :- Parameter Expansion when it is not in POSIX mode?
I tried to check info bash
but only got a copy of the man
page. This is when I learned that I could apt install bash-doc
to get the "full" Bash manual.
After checking both the info pages and the man page, I found this is pretty much the only description of Parameter Expansion:
In each of the cases below, WORD is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
When not performing substring expansion, using the form described below (e.g.,
':-'
), Bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both PARAMETER's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
${PARAMETER:-WORD}
If PARAMETER is unset or null, the expansion of WORD is substituted. Otherwise, the value of PARAMETER is substituted.
$ v=123 $ echo ${v-unset} 123
If I write ${v:-$(some-command)}
the documentation does not specify whether some-command
is run or not. The documentation seems to me to hint that WORD will be expanded whether I want it to be, or not.
I tried some fairly obvious experiments:
$ echo $BASH_VERSION
5.2.15(1)-release
$ something() { echo "something" > /dev/tty; echo "anything" ; }
$ echo ${no_such_var:-$(something)}
something
anything
$ x=1
$ echo ${x:-$(something)}
1
$ echo ${no_such_var:-$(something)}
something
anything
This seems to indicate that this particular version of Bash does not expand the WORD part of the Parameter Expansion unless it is needed.
What I am looking for is a guarantee. POSIX says:
In each case that a value of word is needed (based on the state of parameter, as described below), word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion. If word is not needed, it shall not be expanded.
And Bash promises that it will behave in a compliant fashion when instructed to do so. (Running as sh
, passed --posix
, configured with --enable-strict-posix-dammit-i-really-mean-it-this-time, etc.) But when Bash is running with all the "extra goodness" in not-posix mode, the documentation appears to be silent. This seems like an important point to omit from the documentation, and one where copying the text from the POSIX spec would have been fairly simple.
My question, then, is this: what guarantees does Bash make about order of :- Parameter Expansion when it is not in POSIX mode?
4 comment threads