Understanding $() (and for-loops) in bash
Clash Royale CLAN TAG#URR8PPP
I have a directory with git inside and git branch
gives me the following output:
$ git branch
branch1
* branch2
master
The *
marks the current branch. I would like to use this output in a script. So I loop over the lines like this:
$ for line in $(git branch); do echo "$line"; done;
branch1
README.txt
branch2
master
Questions:
What happened to the *
and why do I see README.txt
[1]?
How can I loop over the lines returned by a normal git branch
exactly as they are, with *
and a space?
P.S.: I am on a Mac right now.
[1] (or any other file in the repository but this one only has one file "README.txt")
bash command-substitution for loop-device
add a comment |
I have a directory with git inside and git branch
gives me the following output:
$ git branch
branch1
* branch2
master
The *
marks the current branch. I would like to use this output in a script. So I loop over the lines like this:
$ for line in $(git branch); do echo "$line"; done;
branch1
README.txt
branch2
master
Questions:
What happened to the *
and why do I see README.txt
[1]?
How can I loop over the lines returned by a normal git branch
exactly as they are, with *
and a space?
P.S.: I am on a Mac right now.
[1] (or any other file in the repository but this one only has one file "README.txt")
bash command-substitution for loop-device
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18
add a comment |
I have a directory with git inside and git branch
gives me the following output:
$ git branch
branch1
* branch2
master
The *
marks the current branch. I would like to use this output in a script. So I loop over the lines like this:
$ for line in $(git branch); do echo "$line"; done;
branch1
README.txt
branch2
master
Questions:
What happened to the *
and why do I see README.txt
[1]?
How can I loop over the lines returned by a normal git branch
exactly as they are, with *
and a space?
P.S.: I am on a Mac right now.
[1] (or any other file in the repository but this one only has one file "README.txt")
bash command-substitution for loop-device
I have a directory with git inside and git branch
gives me the following output:
$ git branch
branch1
* branch2
master
The *
marks the current branch. I would like to use this output in a script. So I loop over the lines like this:
$ for line in $(git branch); do echo "$line"; done;
branch1
README.txt
branch2
master
Questions:
What happened to the *
and why do I see README.txt
[1]?
How can I loop over the lines returned by a normal git branch
exactly as they are, with *
and a space?
P.S.: I am on a Mac right now.
[1] (or any other file in the repository but this one only has one file "README.txt")
bash command-substitution for loop-device
bash command-substitution for loop-device
edited Mar 6 at 15:50
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked Mar 6 at 15:30
scrrrscrrr
288238
288238
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18
add a comment |
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18
add a comment |
1 Answer
1
active
oldest
votes
When $(git branch)
is expanded for the for
loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *
, will be replaced by all filenames in your current directory. This appears to be one file only, README.txt
.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *
:
curr_branch=$( git branch | awk '/^*/ print $2 ' )
Would you want to iterate over the output of git branch
, use a while
loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"n' "$name"
else
printf 'The current branch is "%s"n' "$name"
fi
done
git rev-parse --abbrev-ref HEAD
to show only current branch.
– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504741%2funderstanding-and-for-loops-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When $(git branch)
is expanded for the for
loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *
, will be replaced by all filenames in your current directory. This appears to be one file only, README.txt
.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *
:
curr_branch=$( git branch | awk '/^*/ print $2 ' )
Would you want to iterate over the output of git branch
, use a while
loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"n' "$name"
else
printf 'The current branch is "%s"n' "$name"
fi
done
git rev-parse --abbrev-ref HEAD
to show only current branch.
– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
add a comment |
When $(git branch)
is expanded for the for
loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *
, will be replaced by all filenames in your current directory. This appears to be one file only, README.txt
.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *
:
curr_branch=$( git branch | awk '/^*/ print $2 ' )
Would you want to iterate over the output of git branch
, use a while
loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"n' "$name"
else
printf 'The current branch is "%s"n' "$name"
fi
done
git rev-parse --abbrev-ref HEAD
to show only current branch.
– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
add a comment |
When $(git branch)
is expanded for the for
loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *
, will be replaced by all filenames in your current directory. This appears to be one file only, README.txt
.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *
:
curr_branch=$( git branch | awk '/^*/ print $2 ' )
Would you want to iterate over the output of git branch
, use a while
loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"n' "$name"
else
printf 'The current branch is "%s"n' "$name"
fi
done
When $(git branch)
is expanded for the for
loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *
, will be replaced by all filenames in your current directory. This appears to be one file only, README.txt
.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *
:
curr_branch=$( git branch | awk '/^*/ print $2 ' )
Would you want to iterate over the output of git branch
, use a while
loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"n' "$name"
else
printf 'The current branch is "%s"n' "$name"
fi
done
edited Mar 6 at 15:46
answered Mar 6 at 15:40
Kusalananda♦Kusalananda
139k17260432
139k17260432
git rev-parse --abbrev-ref HEAD
to show only current branch.
– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
add a comment |
git rev-parse --abbrev-ref HEAD
to show only current branch.
– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
git rev-parse --abbrev-ref HEAD
to show only current branch.– pfnuesel
Mar 6 at 15:46
git rev-parse --abbrev-ref HEAD
to show only current branch.– pfnuesel
Mar 6 at 15:46
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
1. Reason I do this I want to also do a $(git config branch.$line.description) and append it to the branch name, but I also want to keep the asterisk. 2. Thank you for this excellent write up!
– scrrr
Mar 6 at 16:07
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504741%2funderstanding-and-for-loops-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"
– glenn jackman
Mar 6 at 17:18