1

I want to display my current git branch in the prompt in ksh88.

I'm working on a legacy project, when the backend run on IBM's AIX, with a mandatory ksh88 shell. I can switch to say, bash, for some operations, but I'm compelled to use ksh88 for most of the daily work. (we are working on changing the shell but it's a very long shot, nigh impossible)

So I'm using this (simplified) kind of variation. all of them work (they display the current branch), but none are dynamic: If I switch branch, they still display the same branch.

export GIT_BRANCH=$(git branch 2>/dev/null | grep '^*' | colrm 1 2)
export PS1="\$GIT_BRANCH - $ "

as well as

get_git_branch(){
    print -- git branch 2>/dev/null | grep '^*' | colrm 1 2
}
export PS1="$(get_git_branch) - $"

In both cases, to update it, I need to reload my .profile (running . ~/.profile), but that's not practical.

When I'm adding the $PWD (in my full version of the export PS1), it does change when I'm changing the current directory

I've read here (How to set the command(s) that get run before every single prompt?) that there could be some tricks to expand some expressions, but only numerical ones.

I've seen this (my biggest hope at the moment) Solaris KSH setting up PS1 with dynamic line but didn't quite understood how to apply it to my problem.

Is there any other tricks I could use ?

3
  • Those "tricks" work with all expressions, not only numeric ones. You're focussing on what's following the $ and not looking at what's around the whole expression. Feb 4, 2022 at 20:32
  • @StéphaneChazelas true! I guess I got confused after spending 2 hours on ksh88 topics and Oracle's documentation ! I edited my question.
    – Rafiki
    Feb 7, 2022 at 16:22
  • @user1686, could you be more specific ? I retried to implement the trick of using an array, but I can't make it work ?
    – Rafiki
    Feb 7, 2022 at 16:22

1 Answer 1

0

Took some puzzel work.

a) use ksh93

b) these aliases give an idea of how to reset (if you want) to not have any branch info in your prompt.

  • I added the following to .profile
ENV=~/.kshrc
export ENV
  • I decided (see above) to keep my aliases in .kshrc. The key lines are:
# alias to reset PS1
alias psr="PS1=$LOGNAME@`hostname`:'\[\${PWD##*/}\]'"
# alias to set PS1 to dynamically check for git branch active, or not
alias psgit="PS1=${LOGNAME}@$(hostname -s)'\[\${PWD##*/}\]{\$(git branch 2>/dev/null| grep \"^*\" | colrm 1 2)}'"

I borrowed from your question!

My default shell is regular ksh(88). Login and switch to the prettier shell looks like this:

aixtools@x064:[aixtools]aixpb
aixtools@x064:[Playbook]psgit
aixtools@x064[Playbook]{$(git branch 2>/dev/null| grep ^* | colrm 1 2)}export PS1
aixtools@x064[Playbook]{$(git branch 2>/dev/null| grep ^* | colrm 1 2)}ksh93
aixtools@x064[Playbook]{master}
aixtools@x064[Playbook]{master}git checkout aixpb_main00
Updating files: 100% (44/44), done.
Switched to branch 'aixpb_main00'
Your branch is up to date with 'aixtools/aixpb_main00'.
aixtools@x064[Playbook]{aixpb_main00}cd
aixtools@x064[aixtools]{}

There is a typo (missing ':') - but that you can fix, as a lot of PS1 is very personal.

Hope this helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .