Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+3
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

4 comment threads

"Only" the man page? (3 comments)
> what guarantees does Bash make about order of :- Parameter Expansion when it is not in POSIX mode? ... (1 comment)
Linux? (1 comment)
Empirical (1 comment)
Linux?

While indeed bash is code, bash discussion tends to happen on linux.codidact.com/ so perhaps this question is better move there so it can get better answers.