Test for a Bash variable being unset, using a function

后端 未结 7 1299
逝去的感伤
逝去的感伤 2020-12-07 11:37

A simple Bash variable test goes:

${varName:?    \"${varName} is not defined\"}

I\'d like to re-use this, by putting it in a function. How

7条回答
  •  一生所求
    2020-12-07 12:06

    Thanks to lhunath's answer, I was led to a part of the Bash man page that I've overlooked hundreds of times:

        When  not performing substring  expansion, bash tests for a parameter that
        is unset  or null; omitting the colon results in a test only for a parame‐
        ter that is unset.
    

    This prompted me to create the following truth table:

    
                    | unset |   set    | set and  | meaning
                    |       | but null | not null |
        ============+=======+==========+==========+=============================
         ${var-_}   |   T   |     F    |    T     | not null or not set
        ------------+-------+----------+----------+-----------------------------
         ${var:-_}  |   T   |     T    |    T     | always true, use for subst.
        ------------+-------+----------+----------+-----------------------------
         $var       |   F   |     F    |    T     | var is set and not null
        ------------+-------+----------+----------+-----------------------------
         ${!var[@]} |   F   |     T    |    T     | var is set
    
    

    This table introduces the specification in the last row. The Bash man page says "If name is not an array, expands to 0 if name is set and null otherwise." For purposes of this truth table, it behaves the same even if it's an array.

提交回复
热议问题