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: Greg Wooledge
Subject: Re: Variable in condtion such [[ ${v-} = 9 ]]
Date: Mon, 28 Aug 2023 14:02:26 -0400

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 ]]

Someone wanted to use 'set -u' but had a variable that might be unset at
run time, so they had to resort to this sort of hack to allow the script
to work.

It remains to be discovered why they wanted to use 'set -u' so badly.
I advise against it, in general.  The "error handling" you get from it
is brutally barbaric -- if an unset variable is used, the script crashes
immediately.  Usually you want something a little more flexible.

Knowing the actual name of the variable in question might have given us
some insight into why the script author made certain decisions, but of
course you chose to omit this information, which makes the situation
harder to diagnose.

If for example the variable name is in ALL_CAPS one might guess that
it's an optional environment variable.  Therefore, its absence would not
be considered a failure, and should not trigger a script crash via
'set -u'.  That still leaves the burning question of why the author
would want to use 'set -u' in such a situation... but again, we lack
the background information needed to answer that.



reply via email to

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