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: Lawrence Velázquez
Subject: Re: Variable in condtion such [[ ${v-} = 9 ]]
Date: Mon, 28 Aug 2023 13:55:01 -0400
User-agent: Cyrus-JMAP/3.9.0-alpha0-701-g9b2f44d3ee-fm-20230823.001-g9b2f44d3

On Mon, Aug 28, 2023, at 1:34 PM, 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

The former accommodates foo being unset in a script using set -u,
and the latter doesn't.

        % bash -uc 'unset foo; [[ ${foo-} = bar ]]; echo "$?"'
        1
        % bash -uc 'unset foo; [[ $foo = bar ]]; echo "$?"'   
        bash: line 1: foo: unbound variable

So the script in question uses set -u, or the author was programming
defensively, or the author saw the idiom somewhere and adopted it
without understanding its purpose.

-- 
vq



reply via email to

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