Skip to contentSkip to conversation

hush parameter expansion ${parameter:+word} is backwards compared to bash

DISCUSSION Bob 1 participant 1 message Dec 13, 2022

lore
Share & tools
lore

Bob

author
#0 lore raw

I notice that the hush parameter expansion ${parameter:+word} is backwards compared to bash.

Here is a test case comparing 'u-boot hush' against 'debian bash'. It shows that at lines 4 and 8, the results are backwards:

shell command result u-boot hush
result debian-bash
------------------------------------- ---------------------
---------------------
1. var=''

2. echo "\${var}       = (${var})"             ${var}       = ()        ${var}       = ()
3. echo "\${var:-bbbb} = (${var:-bbbb})"       ${var:-bbbb} = (bbbb)    ${var:-bbbb} = (bbbb)
4. echo "\${var:+cccc} = (${var:+cccc})"       ${var:+cccc} = (cccc)    ${var:+cccc} = ()

5. var='aaaa'

6. echo "\${var}       = (${var})"             ${var}       = (aaaa)    ${var}       = (aaaa)
7. echo "\${var:-bbbb} = (${var:-bbbb})"       ${var:-bbbb} = (aaaa)    ${var:-bbbb} = (aaaa)
8. echo "\${var:+cccc} = (${var:+cccc})"       ${var:+cccc} = ()        ${var:+cccc} = (cccc)

The bash manual says at: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

${parameter:+word}
------------------
If parameter is null or unset, nothing is substituted, otherwise the
expansion of word is substituted.

Example:
--------

  $ var=123
  $ echo ${var:+var is set and not null}
  var is set and not null