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.

Post History

71%
+3 −0
Q&A 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 m...

1 answer  ·  posted 10mo ago by aghast‭  ·  last activity 10mo ago by Quasímodo‭

Question bash
#3: Post edited by user avatar meta user‭ · 2024-02-16T12:28:00Z (10 months ago)
grammar
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. Which is when I learned that I could `apt install bash-doc` to get the "full" [Bash manual.](https://www.gnu.org/software/bash/manual/bash.html)
  • 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. In fact, 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 do not expand the **WORD** part of the Parameter Expansion unless it is needed.
  • What I am looking for is a guarantee. [POSIX says](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02):
  • > 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?
  • 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.](https://www.gnu.org/software/bash/manual/bash.html)
  • 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](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02):
  • > 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?
#2: Nominated for promotion by user avatar Alexei‭ · 2024-02-14T21:00:45Z (10 months ago)
#1: Initial revision by user avatar aghast‭ · 2024-02-13T22:16:07Z (10 months ago)
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. Which is when I learned that I could `apt install bash-doc` to get the "full" [Bash manual.](https://www.gnu.org/software/bash/manual/bash.html)

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. In fact, 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 do not expand the **WORD** part of the Parameter Expansion unless it is needed. 

What I am looking for is a guarantee. [POSIX says](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02):

> 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?