LMSouq
general-dev Open

How to get the name of the current git branch into a variable in a shell script?

BE
Ben
1 month ago
3 views
Problem Description
I am new to shell scripting and can't figure this out. If you are unfamiliar, the command git branch returns something like * develop master , where the asterisk marks the currently checked out branch. When I run the following in the terminal: git branch | grep "*" I get: * develop as expected. However, when I run test=$(git branch | grep "*") or test=`git branch | grep "*"` And then echo $test , the result is just a list of files in the directory. How do we make the value of test="* develop"? Then the next step (once we get "* develop" into a variable called test), is to get the substring. Would that just be the following? currentBranch=${test:2} I was playing around with that substring function and I got "bad substitution" errors a lot and don't know why.

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base