Sunday, December 7, 2008

Why is my variable disappearing in my script?

It took me a while to figure this out. Sometimes my variable's value did not show up in my script in the following scenarios. It turned out I have a trailing underscore after it, like this:
export VAR1="Hello"
export VAR2="World"

# WRONG: Interpreted as $VAR1_
echo "Create a file called $VAR1_$VAR2.log"

# CORRECT: Use quotes or brackets
echo "Create a file called ${VAR1}_${VAR2}.log"
echo "Create a file called "$VAR1"_"$VAR2".log"

No comments:

Post a Comment