Understanding $() (and for-loops) in bash












0















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")










share|improve this question

























  • See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"

    – glenn jackman
    Mar 6 at 17:18


















0















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")










share|improve this question

























  • See mywiki.wooledge.org/BashFAQ/001, particularly Why you don't read lines with "for"

    – glenn jackman
    Mar 6 at 17:18
















0












0








0








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")










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 15:50









Rui F Ribeiro

41.8k1483142




41.8k1483142










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





















  • 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












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer


























  • 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












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
});


}
});














draft saved

draft discarded


















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









1














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





share|improve this answer


























  • 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














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





share|improve this answer


























  • 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












1








1







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 15:46

























answered Mar 6 at 15:40









KusalanandaKusalananda

138k17258428




138k17258428













  • 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











  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?