firefly
firefly

Reputation: 33

default value for a variable in bash script

I want to give my variable in bash script a default value.

Here is the script :

unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
  DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]]; then
  DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi

kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}

mv KAGGLE_LOCATION .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/

cd data/
unzip *.zip
rm *.zip

When I do echo $DEFAULT_LOCATION, the correct value comes up. But when I do $KAGGLE_LOCATION and dont enter any cmd argument, I get DEFAULT_LOCATION as output instead of the actual value. What is wrong with my code?

PS: I have never used a Mac, so I am not sure if location is same for unix as Mac. If its different, please comment.

Upvotes: 1

Views: 298

Answers (1)

Eby Jacob
Eby Jacob

Reputation: 1458

You are not expanding the variable, try expanding the variable like "$KAGGLE_LOCATION"

unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
  DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]];then
  DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi

kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}

mv "$KAGGLE_LOCATION" .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/

cd data/
unzip *.zip
rm *.zip

Upvotes: 1

Related Questions