help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable in condtion such [[ ${v-} = 9 ]]


From: Emanuele Torre
Subject: Re: Variable in condtion such [[ ${v-} = 9 ]]
Date: Mon, 28 Aug 2023 19:53:55 +0200
User-agent: Mutt/2.2.11 (2023-08-18)

On Tue, Aug 29, 2023 at 12:34:27AM +0700, Budi wrote:
> What is variable in condition such
> 
>  [[ ${foo-} = bar ]]
> 
> as seen in a Linux script of its main package
> 
> why is it if just
>  [[ $foo = bar ]]
> 
> Please give useful info
> 

${foo-xyz} expands to "xyz" if the variable foo is not set.

Normally ${foo-} would be the same as just $foo since an unset variable
expands to the empty string.

When you see ${foo-} in script, that likely means that the script is
using the   set -o nounset  (a.k.a.  set -u )  setting.

That setting causes expansions for unset variables to trigger a fatal
error and exit the script immediately. Some people like to use it in
scripts for some reason.

So, basically, ${foo-} is exactly like $foo, but it does not trigger an
error if the variable foo is not set when using the nounset setting. You
use it to bypass nounset for that specific expansion.

In that script, the person who wrote it wanted to check if  ${foo-}  was
equal to "bar", and not trigger a fatal error if the foo variable is
unset for that specific check.

o/
 emanuele6



reply via email to

[Prev in Thread] Current Thread [Next in Thread]