Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:39:09+00:00 2026-05-13T12:39:09+00:00

I am new to shell scripting and can’t figure this out. If you are

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:39:09+00:00Added an answer on May 13, 2026 at 12:39 pm

    The * is expanded, what you can do is use sed instead of grep and get the name of the branch immediately:

    branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
    

    And a version using git symbolic-ref, as suggested by Noufal Ibrahim

    branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
    

    To elaborate on the expansion, (as marco already did,) the expansion happens in the echo, when you do echo $test with $test containing * master then the * is expanded according to the normal expansion rules. To suppress this one would have to quote the variable, as shown by marco: echo "$test". Alternatively, if you get rid of the asterisk before you echo it, all will be fine, e.g. echo ${test:2} will just echo master. Alternatively you could assign it anew as you already proposed:

    branch=${test:2}
    echo $branch
    

    This will echo master, like you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to Unix shell scripting. Here's my problem. I've used this script...
Forgive my ignorance, but I am somewhat new to shell scripting and the use
I am new with shell scripting. I am having a file containing some records
I'm fairly new at programming, but I've wondered how shell text editors such as
New to javascript/jquery and having a hard time with using this or $(this) to
New to silverlight. Traditionally if I were to design a wizard-like process where a
New to WCF, but familiar with COM+ - can I wrap a WCF service
The new extensions in .Net 3.5 allow functionality to be split out from interfaces.
I'm new to bash scripts (and the *nix shell altogether) but I'm trying to
I am following this tutorial that is like the hello world for post-commit I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.