Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • (1) I can’t reproduce the results you show in your first code block: declare name followed by declare -p name yields “bash: declare: name: not found”.  (Although declare -p | grep na yields declare -- name.) (2) I believe that it’s a little bit misleading to present echo "${name-isunset}" in the context of declare name, inasmuch as it treats an undeclared (i.e., undefined) variable the same as a declared but unset variable. (3) You might want to mention that namerefs are available only in bash version 4.3 and higher. Commented Jun 22, 2019 at 18:57
  • @G-Man Thank you for your remarks! I'll address them as soon as I can and I'll update my answer where appropriate. As for (1), my GNU bash, version 5.0.7(1)-release (x86_64-pc-linux-gnu) on Arch Linux still yields the results I showed. Maybe that behavior has been introduced only recently, I'll investigate that. Commented Jun 22, 2019 at 19:43
  • Yeah; I'm only using version 4.3. Commented Jun 22, 2019 at 20:12
  • 2
    @G-Man Answer updated with notes about (1) and (3). About (2): I aimed to illustrate that declare x doesn't set x, while declare x= does. I couldn't find any reference to back the assertion that declare -- x (as output of declare -p x) means "not set", while declare -- x="" means "set"; thus I brought in the ${parameter-word} expansion, even if it cannot discriminate between "unset" and "doesn't exist", as you point out. I'm not sure how can I clarify this in my answer (without distracting the reader from the point), though. Commented Jun 23, 2019 at 10:34
  • For (2), 1. If someone don't know what -- means since help declare doesn't show about that, in info bash it says "each builtin command documented as accepting options preceded by '-' accepts '--' to signify the end of the options". 2. The doc also says "If a variable name is followed by =VALUE, the value of the variable is set to VALUE" which may implicitly mean if not, then not set. Commented Jan 3 at 11:33