0

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?

1 Answer 1

1

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

1 Comment

Yep. Removing spaces around = sign fixed it. Thanks a lot.

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.