Test if a variable is set in bash when using “set -o nounset”

前端 未结 6 1724
北海茫月
北海茫月 2020-12-07 13:28

The following code exits with a unbound variable error. How to fix this, while still using the set -o nounset option?

#!/bin/bash

set -o nounse         


        
6条回答
  •  鱼传尺愫
    2020-12-07 13:40

    You can use

    if [[ ${WHATEVER:+$WHATEVER} ]]; then
    

    but

    if [[ "${WHATEVER:+isset}" == "isset" ]]; then
    

    might be more readable.

提交回复
热议问题