You are not logged in.

#1 2022-11-23 05:06:47

Firestar
Member
From: Beijing, China
Registered: 2021-07-19
Posts: 49
Website

my bash PS1 cannot get git branch

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

#2 2022-11-23 05:23:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,570
Website

Re: my bash PS1 cannot get git branch

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

#3 2022-11-23 05:46:56

Firestar
Member
From: Beijing, China
Registered: 2021-07-19
Posts: 49
Website

Re: my bash PS1 cannot get git branch

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

#4 2022-11-23 05:56:03

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,294
Website

Re: my bash PS1 cannot get git branch

What is the output of `git branch` in CWD?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2022-11-23 06:01:11

Firestar
Member
From: Beijing, China
Registered: 2021-07-19
Posts: 49
Website

Re: my bash PS1 cannot get git branch

jasonwryan wrote:

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

#6 2022-11-23 06:18:42

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 6,840

Re: my bash PS1 cannot get git branch

Firestar wrote:

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

#7 2022-11-23 07:12:36

Firestar
Member
From: Beijing, China
Registered: 2021-07-19
Posts: 49
Website

Re: my bash PS1 cannot get git branch

cfr wrote:

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

#8 2022-11-23 09:47:08

seth
Member
Registered: 2012-09-03
Posts: 33,623

Re: my bash PS1 cannot get git branch

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

#9 2022-11-23 12:10:15

annabanana
Member
From: cambridge / york - england
Registered: 2022-11-23
Posts: 1
Website

Re: my bash PS1 cannot get git branch

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

Board footer

Powered by FluxBB