Interesting Bash Prompts
maketecheasier.com
maketecheasier.com
Don't ever use ">" in your prompts, particularly as root.
A coworker had spent several days working on* an important script with a tight deadline that was almost upon him, and with a quick slip of the right mouse button, pasted what he had selected before (several lines of window scrollback) into the window to be executed, including:
/tmp/foo> ./blah.pl
and just like that, blah.pl ended up zero length. We hard killed the machine and scanned the disk for the contents of the script and recovered most of it, but had the specifics been different we could have been a lot less lucky.
For this reason it's probably also a good idea to avoid question marks, asterisks, and anything else the shell could expand, as well.
* While the story plays just as well, it's not meant to be an argument for source control. It could have just as easily been full paths to system binaries and have ended up trashing the system.
Source is http://www.faqs.org/docs/Linux-HOWTO/Bash-Prompt-HOWTO.html#...
jcs@smug:~% echo asdf > file
jcs@smug:~% echo fdsa > file
file: File exists.
jcs@smug:~% echo fdsa >! file
jcs@smug:~%
this has saved me quite a few times when accidently typing > instead of >> or similar.For example, when using typical Unix terminals, I refuse to mouse copy/paste when logged in as root (whether or not the prompt is nearby), because it's too easy to make a mistake and the consequences are severe. Similarly, I refuse to rely on any cute shell tricks like "!" to pull history items when logged in as root. Sacrifice convenience to guarantee safety.
I also prefer to use my Mac terminal (MacTelnet), which will trap multi-line pastes and offer options to paste as-is, paste as one line, or cancel the operation. This has saved me more than once.
(Certainly there are a few things I don't know about administering large groups of machines. However, I've been running my own personal Linux boxes for a while. And the last time I logged in as root was a bit over 9 years ago.)
* I don't need the hostname. The lack of it = my machine.
* I don't need the username. I'm the only user.
* I don't need the full path. I know my machine pretty well. If I'm in doubt, pwd is half a second away.
* The prompt is red so I easily can scan what the output of this specific command was.
* Current Git-branch = helps me not make stupid mistakes.
gitprompt() {
PS1="\[\e[0;31m\]\W$(__git_ps1 " \[\e[1;31m\](%s)") \$ \[\e[m\]"
}
PROMPT_COMMAND=gitprompt PS1='[%(?..%?)%(?.. ! )$PR_GREEN%2>>%n%<<@%m$PR_NO_COLOR $PR_BLUE%30<...<%~%<<$PR_NO_COLOR]%(!.#.$) '
path is limited to 30 characters and the front is trimmed with '...'. There is a host, because I work on at least 5 at the same time every day, but the username is cut to only the first 2 letters (so it's either vi, or ro, or something else at uni). The return code is only visible when it's non-zero.http://aperiodic.net/phil/prompt/
Once you go bash->zsh, there is no turning back.
[rupert] ~$ uname -a
SunOS rupert 5.11 NexentaOS_20081207 i86pc i386 i86pc Solaris
[rupert] ~$ sudo apt-get install zsh
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package zsh is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package zsh has no installation candidate Last login: Sun Sep 6 12:01:35 2009 from 192.168.1.113
Sun Microsystems Inc. SunOS 5.10 Generic January 2005
bash-3.00$ zsh
unknown%
That said, I prefer Fish when I can get it: http://fishshell.org/index.php # pkgutil --install zshOpened up zsh, and it throws me into a new-user setup thing, stepping me through a bunch of different configuration options. I'm impressed so far. :P
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
function parse_current_dir {
ruby -e "puts ('../'+Dir.getwd.split('/').last(2).join('/')).gsub('//', '/')"
}
export PS1='\[\033[31;40m\]$(parse_current_dir)\[\033[0;33m\]$(parse_git_branch)\[\e[0m\]$ '
I couldn't be bothered to work out how to shorten the path natively, so I hacked it [badly] ]in ruby... :) davidw@fortrock:~/workshop/langpop (master) $Here's my bash prompt script. It shows the command number from this session (PS_NUM), the exit code of the last command (highlighted red if != 0), the pwd (in cyan), and the git branch (in magenta)
OFF="\[\033[0m\]"
RED="\[\033[0;31m\]"
MAGENTA="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
function promptcommand {
STATUS=$?
PS_NUM="[\#]"
PS_STATUS="[$STATUS]"
PS_PWD="${CYAN}\w${OFF}"
PS_GITBRANCH="${MAGENTA}\$(__git_ps1)${OFF}"
PS_PROMPT="\$"
if [ $STATUS -ne "0" ]; then
PS_PROMPT="${RED}${PS_PROMPT}${OFF}"
fi
PS1="${PS_NUM}${PS_STATUS} ${PS_PWD}${PS_GITBRANCH} ${PS_PROMPT} "
}
PROMPT_COMMAND=promptcommand
I probably could have eliminated the PROMPT_COMMAND thing like in the first example.It's a Ruby script so not the fastest in theory, but I haven't noticed any delay using Ruby 1.9
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
Here's the code for the zsh prompt that uses it: http://bitbucket.org/durin42/dotfiles/src/tip/.shell.d/99.pr...
PS1='C:${PWD//\//\\\}>'With much messing around, I managed to figure out how to replicate the same functionality in bash:
http://screwtape.jottit.com/how_to_limit_the_length_of_your_bash_promptWith TTYs and their CRT equivalents, there really wasn't any alternative. However, most folks have their command line in a window these days, and windows have other places to put state information.