Live data from Hacker News

Bash PS1 Generator

bashrcgenerator.com

51–60 of 146 posts

Re: Bash PS1 Generator

#52

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)]\$ "

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.

Re: Bash PS1 Generator

#53

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?

`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.

Re: Bash PS1 Generator

#54

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?

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.

Re: Bash PS1 Generator

#55
post #52

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)]\$ "

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.

makes sense. thanks!

Re: Bash PS1 Generator

#56

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)]\$ "

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

Re: Bash PS1 Generator

#58
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.

Re: Bash PS1 Generator

#60
post #36

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).

Yea, why not just have "double-click" as "add this element to the end of input".

Why not just single click?
Post reply on HN