[prev in list] [next in list] [prev in thread] [next in thread]
List: bash-bug
Subject: Re: Bash reference manual: shell parameter expansion examples
From: Chet Ramey <chet.ramey () case ! edu>
Date: 2024-02-23 19:45:49
Message-ID: 34c5d3dd-c0fe-4a84-909f-d5d71cdb6f18 () case ! edu
[Download RAW message or body]
[Attachment #2 (multipart/mixed)]
[Attachment #4 (text/plain)]
On 2/23/24 10:44 AM, James Weigle wrote:
> Hi!
>
> In the Bash reference manual, there are a series of examples of testing if
> a variable is null or unset:
>
> - Under *${parameter:-word}*, the usage is *${var-unset}*.
> - Under *${parameter:=word}*, the usage is *${var:=DEFAULT}*.
> - Under *${parameter:?word}*, the usage is *${var:?var is unset or null}*
> .
> - Under *${parameter:+word}*, the usage is *${var:+var is set and not
> null}*.
>
> I got a little confused at the first example, because it's the *only*
> example where the colon is omitted. It still works—but why is that one
> using a different form?
After this paragraph:
"When not performing substring expansion, using the forms 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."
that you noted, the current manual has these:
$ v=123
$ echo ${v-unset}
123
$ echo ${v:-unset-or-null}
123
$ unset v
$ echo ${v-unset}
unset
$ v=
$ echo ${v:-unset-or-null}
unset-or-null
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
["OpenPGP_signature.asc" (application/pgp-signature)]
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic