I wants to list git branches in a set of directories using bash.
Now I just want to format the branch name differently if it's "master". In other words, if the branch is 'master' I want to display it in Blue or any other color.
There's no need to cd
around for this. Just use something like this
reset=$(tput sgr0)
blue=$(tput setaf 4)
for dir in directory_name_1 directory_name_2 ...; do
branch=$(GIT_DIR="$dir/.git" git symbolic-ref --short HEAD)
color=
if [ master = "$branch" ]; then
color=$blue
fi
printf '%s: %s%s%s\n' "${dir##/*}" "$color" "$branch" "$reset"
done
Use tput
to get the correct control codes for the current terminal for reset attributes ( sgr0
) and for set foreground color ( setaf
) to blue (default color 4).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.