[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bash reference manual: shell parameter expansion examples
From: |
Chet Ramey |
Subject: |
Re: Bash reference manual: shell parameter expansion examples |
Date: |
Fri, 23 Feb 2024 14:45:49 -0500 |
User-agent: |
Mozilla Thunderbird |
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
Description: OpenPGP digital signature