You are not logged in.
Pages: 1
I configure bash PS1 from https://bashrcgenerator.com/
and its PS1 to get git branch is:
export PS1="\$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')\[$(tput sgr0)\]"
but it cannot get git branch and display only `()`
()git branch 2>/dev/null
master
()
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
Offline
First I'd simplify that. You use sed to delete everything but the branch highlighted with an asterisks which is fragile: if git every changes the means of highlighting the current branch, this will break (and perhaps that's part of the problem if there is a space before the asterisk, etc). You can ask git just for the current branch and not worry about deleting non-asterisk-marked lines and then deleting the asterisk too:
git branch --show-current 2>/dev/null
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
OK I know. change it to:
export PS1="\$(git branch --show-current 2> /dev/null)\[$(tput sgr0)\]"
but I want it display nothing when this is not a git repo.
Last edited by Firestar (2022-11-23 06:00:21)
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
Offline
What is the output of `git branch` in CWD?
Offline
What is the output of `git branch` in CWD?
I said in the first post. It is `master`
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
Offline
OK I know. change it to:
export PS1="\$(git branch --show-current 2> /dev/null)\[$(tput sgr0)\]"
but I want it display nothing when this is not a git repo.
So what does that command display when it isn't a git repo?
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
So what does that command display when it isn't a git repo?
in fact I added a `() ` and it will display
(master) [username @ hostname ] $
when it is a git repo
and it will display
() [username @ hostname ] $
when it isn't a git repo and I do not want the empty brackets
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
Offline
This means there's some issue w/ your sed.
Throwing stuff at the wall:
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
type sed
git branch 2> /dev/null | /usr/bin/sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
pacman -Qs sed
localectl
locale
locale -a
Offline
i wrote this quick function, should do what you want i think:
get_git_branch() {
out=$(git branch --show-current 2> /dev/null)
if [[ $? -eq 0 ]]; then
printf "(%s) " "$out"
fi
}
export PS1="\$(get_git_branch)\u@\h [\W]\[$(tput sgr0)\] >"
rust, neovim, C, game development
perma chilling
she/they
Offline
Pages: 1