0

I am new to bash programming and I am trying to setup a bash script, and I require to store the value of the path of the script, but as I can see, it gets lost.

#!/bin/bash

dir = "$( cd "$( dirname "${BASH_SOURCE[0]}"  )" && pwd  )"

echo "$dir"

echo "
|------------------------------------|
|------------------------------------|
|                                    |
|Welcome to ec2 setup!!! Let's begin |
|                                    |
|------------------------------------|
|------------------------------------|
"

echo "
/-----------------------------

Do you want to install nodejs?

-----------------------------/"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) echo "Enter the version number, eg; 8, 9"
            read version
            curl -sL https://deb.nodesource.com/setup_$version.x | sudo -E bash
            sudo apt-get install -y nodejs
            echo "Node version installed is $(node -v)"; break;;
        No ) break;;
    esac

    done

echo "$dir"

echo "
/-----------------------------

Do you want to install mongodb?

-----------------------------/"

select yn in "Yes" "No"; do
    case $yn in
        Yes ) sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
            echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
            sudo apt-get update
            sudo apt-get install -y mongodb-org
            sudo service mongod start; break;;
        No ) break;;
    esac
done

echo "$dir"

When I run this script, echo "$dir" prints nothing, except for at the start of the script.

What is it am I missing here?

Also, later in the code, I have to change directories, so how do I go about maintaining th reference to the initial directory?

relentless-coder
  • 1,478
  • 3
  • 20
  • 39

1 Answers1

1

You can't have spaces in your variable declarations between the variable name and the =,

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" 
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Please vote-to-close frequently-asked-questions as duplicate, vs. answering them. See also https://stackoverflow.com/help/how-to-answer, particularly the section "Answer Well-Asked Questions", particularly the bullet point regarding questions which "...have already been asked and answered many times before". – Charles Duffy Mar 15 '18 at 16:40
  • Please do not expect others to share your disregard for published site rules, or to act in a manner that respects it. – Charles Duffy Mar 15 '18 at 16:42
  • @CharlesDuffy I don't at all, you should do as you please, this isn't a tag I generally follow, how should I know what is a a dupe or not – Rorschach Mar 15 '18 at 16:42
  • @CharlesDuffy sorry, I didn't know the exact error that I was looking for since the first `echo $dir` did print the correct value. – relentless-coder Mar 15 '18 at 16:50