Skip to main content
added 510 characters in body
Source Link
l0b0
  • 53.6k
  • 48
  • 225
  • 398

Text streams like this should be read using a while loop rather than for, which is probably causing the issue (although I cannot reproduce it). A simple way to do this:

git branch | grepwhile IFS= read -vr line
do
    echo "${line:2}"
done

Comparison:

$ cd -- "$(mktemp -d)"
$ git init
Initialized empty Git repository in /tmp/tmp.MJFmu7q7EH/.git/
$ touch README
$ git commit --allow-empty -m "Initial commit"
[master (root-commit) da46b03] Initial commit
$ git branch foo
$ git branch
  foo
* master
$ for i in $(git branch)
> do
>    echo $i
> done
foo
*
master
$ git branch | while IFS= read -r line
> do
>     echo "${line:2}"
> done
foo
master

Text streams like this should be read using a while loop rather than for, which is probably causing the issue. A simple way to do this:

git branch | grep -v master | while IFS= read -r line
do
    echo "${line:2}"
done

Text streams like this should be read using a while loop rather than for, which is probably causing the issue (although I cannot reproduce it). A simple way to do this:

git branch | while IFS= read -r line
do
    echo "${line:2}"
done

Comparison:

$ cd -- "$(mktemp -d)"
$ git init
Initialized empty Git repository in /tmp/tmp.MJFmu7q7EH/.git/
$ touch README
$ git commit --allow-empty -m "Initial commit"
[master (root-commit) da46b03] Initial commit
$ git branch foo
$ git branch
  foo
* master
$ for i in $(git branch)
> do
>    echo $i
> done
foo
*
master
$ git branch | while IFS= read -r line
> do
>     echo "${line:2}"
> done
foo
master
Source Link
l0b0
  • 53.6k
  • 48
  • 225
  • 398

Text streams like this should be read using a while loop rather than for, which is probably causing the issue. A simple way to do this:

git branch | grep -v master | while IFS= read -r line
do
    echo "${line:2}"
done