Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k
added 9 characters in body
Source Link
IQAndreas
  • 10.7k
  • 22
  • 65
  • 81

Say I have the following code:

# Check if the color prompt is enabled and supported on this system
if [ -n "$force_color_prompt" ] && [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    GREEN="\033[1;32m"
    DIM="\033[2m"
    RESET="\033[00m"
fi

echo -e "Oh, ${GREEN}green${RESET} world, ${DIM}don't desert me now...${RESET}"

If color support is enabled, it will echo out a pretty, colored line. If color support isn't enabled, values like ${GREEN} will not have been set, and the text will print out in the usual white with black background.

The code relies on the fact that variables that aren't set will simply evaluate to an empty string (which in my tests, they do). Will this cause bugs or problems on some systems, or will all non-existent variables always evaluate to an empty string? Is there any reason I shouldn't rely on this mechanic?

Say I have the following code:

# Check if the color prompt is enabled and supported on this system
if [ -n "$force_color_prompt" ] && [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    GREEN="\033[1;32m"
    DIM="\033[2m"
    RESET="\033[00m"
fi

echo -e "Oh, ${GREEN}green${RESET} world, ${DIM}don't desert me now...${RESET}"

If color support is enabled, it will echo out a pretty, colored line. If color support isn't enabled, values like ${GREEN} will not have been set, and the text will print out in the usual white with black background.

The code relies on the fact that variables that aren't set will simply evaluate to an empty string (which in my tests, they do). Will this cause bugs or problems on some systems, or will all non-existent variables always evaluate to an empty string? Is there any reason I shouldn't rely on this?

Say I have the following code:

# Check if the color prompt is enabled and supported on this system
if [ -n "$force_color_prompt" ] && [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    GREEN="\033[1;32m"
    DIM="\033[2m"
    RESET="\033[00m"
fi

echo -e "Oh, ${GREEN}green${RESET} world, ${DIM}don't desert me now...${RESET}"

If color support is enabled, it will echo out a pretty, colored line. If color support isn't enabled, values like ${GREEN} will not have been set, and the text will print out in the usual white with black background.

The code relies on the fact that variables that aren't set will simply evaluate to an empty string (which in my tests, they do). Will this cause bugs or problems on some systems, or will all non-existent variables always evaluate to an empty string? Is there any reason I shouldn't rely on this mechanic?

Source Link
IQAndreas
  • 10.7k
  • 22
  • 65
  • 81

Is there any harm in using variables that aren't set?

Say I have the following code:

# Check if the color prompt is enabled and supported on this system
if [ -n "$force_color_prompt" ] && [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    GREEN="\033[1;32m"
    DIM="\033[2m"
    RESET="\033[00m"
fi

echo -e "Oh, ${GREEN}green${RESET} world, ${DIM}don't desert me now...${RESET}"

If color support is enabled, it will echo out a pretty, colored line. If color support isn't enabled, values like ${GREEN} will not have been set, and the text will print out in the usual white with black background.

The code relies on the fact that variables that aren't set will simply evaluate to an empty string (which in my tests, they do). Will this cause bugs or problems on some systems, or will all non-existent variables always evaluate to an empty string? Is there any reason I shouldn't rely on this?