Ajay Kumar
Ajay Kumar

Reputation: 3272

Git-bash string variable is returned as null

The question is related to this and 100s of similar other SO threads. I have a variable like and its usage like below:

branchtochechout = "develop"

echo "Branch To Chechout is : $branchtochechout" 

for thedir in ./*/ ; 
 do ( 
  echo -e "Checking out in the directory:$thedir"; 
  cd "$thedir" && git checkout $branchtochechout); 
  sleep 2
 done

To my surprise, $branchtochechout gives me null in the echo statement as well in the for loop. Does this git-bash (windows) not work similar to linux bash scripting? Please help me understand what I am doing wrong here?

Upvotes: 0

Views: 152

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15418

You can't put spaces around the = in a variable assignment.
Also, quote your vars, and add a shebang.
Try https://www.shellcheck.net/

Line 1:
branchtochechout = "develop"
^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
                 ^-- SC2283 (error): Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).

Line 3:
echo "Branch To Chechout is : $branchtochechout"
                              ^-- SC2154 (warning): branchtochechout is referenced but not assigned.

Line 8:
  cd "$thedir" && git checkout $branchtochechout);
                               ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
  cd "$thedir" && git checkout "$branchtochechout");

Upvotes: 1

Related Questions