0

The command

$ git branch --contains HEAD

returns

* goofing_around

as I expect. But I want to store this in a bash variable, so I run

$ bbb=$(git branch --contains HEAD)

and then I check the contents of bbb ...

$ echo $bbb

... and get ...

file1 file2 file3 goofing_around

(file1 etc. are indeed files in the directory.) What is going on? How can I store just that first line that gives me the current branch name?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • Nothing wrong with how you're storing it. What's wrong is how you're checking its contents. – Charles Duffy Jan 23 '17 at 18:57
  • 1
    BTW, http://shellcheck.net/ would have caught this for you. – Charles Duffy Jan 23 '17 at 18:59
  • As another aside, consider `declare -p bbb` as a more precise way to check what the variable `bbb` contains: Its output will tell you whether the variable is exported; whether it's an array; whether it's been set read-only, flagged as an integer, etc. – Charles Duffy Jan 23 '17 at 19:01

1 Answers1

2

Quotes are important: Unquoted expansions are string-split (broken into words on spaces, or characters in IFS) and glob-expanded (so a word containing only * is replaced with a list of filenames in the current directory).

echo "$bbb"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441