[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bash reference manual: shell parameter expansion examples
From: |
James Weigle |
Subject: |
Bash reference manual: shell parameter expansion examples |
Date: |
Fri, 23 Feb 2024 10:44:42 -0500 |
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?
I suppose otherwise there would be no example showing the usage without a
colon. But could the example incorporate both ways, like so?
$ v=123
> echo ${v-does not exist}
> 123
> $ v=
> $ echo ${v-does not exist}
>
> $ echo ${v:-null or does not exist}
> null or does not exist
> $ unset v
> $ echo ${v-does not exist}
> does not exist
>
But that’s a bit verbose. One other way to do it would be to just add the
colon to the original example (it has the same behavior in this case)...
$ v=123
> $ echo ${v:-unset}
> 123
>
and just let the explanation of the '-' vs. ':-' usage suffice, in the
paragraph before these examples. What do you think?
Best,
James
- Bash reference manual: shell parameter expansion examples,
James Weigle <=