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)
"Only" the man page?
Peter Taylor‭ wrote 8 months ago

Off topic, but when you say "only got a copy of the man page", is that not pretty exhaustive already? I remember that when I printed man bash a bit more than two decades ago, the result was a big stack of paper (180 or so pages IIRC).

aghast‭ wrote 8 months ago

No. GNU decided at some point that man pages were "so last century," and that all the good info would be in emacs^W^W in the "info" browser. So if you check "man some-gnu-program" there's a fair chance the last paragraph will be something like "we've provided this man page as a courtesy, but the real deets are only found using info some-gnu-program."

In this case, I hadn't installed the -doc package, and apparently there is a separate doc package, so the info file was just the same content as the man page, a minimal summary of command line switches and such. The 180 page thing you're thinking of, the "GNU Bash Manual" or whatever they're calling it, was only installed after I requested the bash-doc Debian package.

aghast‭ wrote 8 months ago · edited 8 months ago

Here's an excerpt from man tar:

NOTE

This manpage is a short description of GNU tar. For a detailed discussion, including examples and usage recommendations, refer to the GNU Tar Manual available in texinfo format. If the info reader and the tar documentation are properly installed on your system, the command

info tar

should give you access to the complete manual.

You can also view the manual using the info mode in emacs(1), or find it in various formats online at

http://www.gnu.org/software/tar/manual

If any discrepancies occur between this manpage and the GNU Tar Manual, the later shall be considered the authoritative source.