1

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.

2
  • 1
    You aren't expanding the values like you should be: KAGGLE_LOCATION=${2:-$DEFAULT_LOCAITON} and mv "$KAGGLE_LOCATION" .. Commented May 5, 2018 at 13:27
  • Thanks, that worked. Commented May 5, 2018 at 13:29

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Similarly, you need a $ in front of DEFAULT_LOCATION when assigning to KAGGLE_LOCATION.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.