Skip to main content
added 394 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

In POSIX shell, you can use setset -u -u/ set -o nounset, which will cause the shell to thrown an error when trying to expand an unset variable:

#!/bin/sh

set -u
: "${UNSET_VAR}"

E.g. in Bash, it will thrown an error like unset1.sh: line 3: UNSET_VAR: unbound variable

or using the ${var?message} Parameter Expansion:, which will thrown an error with the given message if the variable is unset.

: "${UNSET_VAR?Unset variable}"

In your case, you should use :? instead of ? to also fail on set but empty variables:

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

set -u doesn't have an equivalent for erroring on variables set to the empty string.

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?Unset variable}"

In your case, you should use :? instead of ? to also fail on set but empty variables:

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

In POSIX shell, you can use set -u / set -o nounset, which will cause the shell to thrown an error when trying to expand an unset variable:

#!/bin/sh

set -u
: "${UNSET_VAR}"

E.g. in Bash, it will thrown an error like unset1.sh: line 3: UNSET_VAR: unbound variable

or using the ${var?message} Parameter Expansion, which will thrown an error with the given message if the variable is unset.

: "${UNSET_VAR?Unset variable}"

In your case, you should use :? instead of ? to also fail on set but empty variables:

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

set -u doesn't have an equivalent for erroring on variables set to the empty string.

added 2 characters in body
Source Link
cuonglm
  • 158.2k
  • 41
  • 342
  • 420

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?Unset variable}"

or inIn your case (note the, you should use :? instead of ? to also fail on set but empty variables):

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?Unset variable}"

or in your case (note the :? instead of ? to also fail on set but empty variables):

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?Unset variable}"

In your case, you should use :? instead of ? to also fail on set but empty variables:

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*
added 155 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?"UnsetUnset variable"variable}"

or in your case (note the :? instead of ? to also fail on set but empty variables):

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?"Unset variable"}"

In POSIX shell, you can use set -u:

#!/bin/sh

set -u
: "${UNSET_VAR}"

or using Parameter Expansion:

: "${UNSET_VAR?Unset variable}"

or in your case (note the :? instead of ? to also fail on set but empty variables):

rm -rf -- "${PROJECT_HOME:?PROJECT_HOME empty or unset}"/*
Source Link
cuonglm
  • 158.2k
  • 41
  • 342
  • 420
Loading