Comments on: What’s in your Bash Prompt? http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: Ingvar http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-134428 Mon, 08 Sep 2014 16:52:33 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-134428

What I should have is:

PS1=”: \u@\h\$ ;”

Because that means I can simply cut&paste a whole line and it is still a valid command. Triple-clicking (in xterm, at least) is an easy-ish way to get a whole line, so…

Reply  |  Quote
]]>
By: captnjlp http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-80788 Tue, 22 Apr 2014 23:43:22 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-80788

Sorry, one more… I was getting weird linewrap issues using the above code (illustrative GIF: http://i.stack.imgur.com/fccjc.gif). Because I had my color codes defined as:

##-ANSI-COLOR-CODES-##
Color_Off="\[33[0m\]"
###-Regular-###
Red="\[33[0;31m\]"
Green="\[33[0;32m\]"
Purple="\[33[0;35\]"
Yellow="\[33[1;33m\]"
####-Bold-####
BRed="\[33[1;31m\]"
BPurple="\[33[1;35m\]"

lines like
PS1+="\[$Green\][\!]\[$Color_Off\] "
were causing the escape sequences to be double quoted. The article’s a little unclear as to the final color definitions you arrived at (it also doesn’t define $Yellow despite its use in the SSH_CLIENT code). If somebody’s reading this and defines their colors as I do, I’ll save you the trouble of removing the extra escape sequences:


# set up command prompt
function __prompt_command()
{
# capture the exit status of the last command
EXIT="$?"
PS1=""
if [ $EXIT -eq 0 ]; then PS1+="$Green[\!]$Color_Off "; else PS1+="$Red[\!]$Color_Off "; fi
# if logged in via ssh shows the ip of the client
if [ -n "$SSH_CLIENT" ]; then PS1+="$Yellow(${SSH_CLIENT%% *})$Color_Off"; fi
# debian chroot stuff (take it or leave it)
PS1+="${debian_chroot:+($debian_chroot)}"
# basic information (user@host:path)
PS1+="$BRed\u$Color_Off@$BRed\h$Color_Off:$BPurple\w$Color_Off "
# check if inside git repo
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
# parse the porcelain output of git status
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local Color_On=$Green
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local Color_On=$Purple
else
local Color_On=$Red
fi
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD`)"
fi
# add the result to prompt
PS1+="$Color_On[$branch]$Color_Off "
fi
# prompt $ or # for root
PS1+="\$ "
}
PROMPT_COMMAND=__prompt_command
@ captnjlp:

Reply  |  Quote
]]>
By: captnjlp http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-80773 Tue, 22 Apr 2014 23:15:20 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-80773

Thanks for posting this; saved me inordinate amounts of time. However, the SSH_CLIENT line creates a “bad substitution” error on every prompt. Removing the extra dollar sign (and the unescaped quotation marks) fixed it for me:


if [ -n "$SSH_CLIENT" ]; then PS1+="\[$Yellow\](${SSH_CLIENT%% *})\[$Color_Off\]"; fi

Reply  |  Quote
]]>
By: John http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-70056 Mon, 07 Apr 2014 16:19:51 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-70056

Color me unimpressed. The lengths some hipsters will go to in order to feel unique. A prompt is supposed to be functional, not distracting.

Reply  |  Quote
]]>
By: D http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-62759 Sat, 25 Jan 2014 16:24:16 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-62759

Brilliant look for the PS1, love the idea.
Just an FYI for people ending up here using Ubuntu.
When I tried to use the above it didn’t work for me (Ubuntu 13.10). This is because Ubuntu has been using a different shell since 6.10 (https://wiki.ubuntu.com/DashAsBinSh).
Basically it means that some of the things (like the substitutions) wont work. I haven’t figured out how to get it to adjust the above code to make it work. But if someone does: post it!

Reply  |  Quote
]]>
By: LIAR http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-57271 Wed, 13 Nov 2013 14:34:39 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-57271

Hey!

I’ve fighted hours with the bad wrapping of lines due to the use of colors in PS1.

The best solution I found:
https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14800

And yes the solution is as simple as using 01 and 02 instead of \[ and \]!!!!

Unfortunately, it is very difficult to find, since it is not discussed in bash texts/blogs/articles/…, but in an R lang bug report!!!

Hope this helps!

Reply  |  Quote
]]>
By: Aske Olsson http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-57197 Mon, 11 Nov 2013 10:02:01 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-57197

Why don’t you use some of the other git ps1 vars like:

GIT_PS1_SHOWDIRTYSTATE
GIT_PS1_SHOWUPSTREAM

Check them out here

Reply  |  Quote
]]>
By: larsmans http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-26134 Tue, 22 Jan 2013 15:54:11 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-26134

Checking for exit status 0 isn’t enough; when you type Ctrl+Z to push a command into the background (“stop” it, in POSIX terms), $? gets set to a non-zero value. The solution is to check for

[ $? -eq 0 -o `kill -l $?` = TSTP ]

Reply  |  Quote
]]>
By: Chris http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-26133 Tue, 22 Jan 2013 14:29:11 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-26133

I’m often confronted with a lack of screen space, so I tend to go with the minimalistic:
PS1="\$ "
as it means you don’t have anything taking up valuable console width.

Reply  |  Quote
]]>
By: Scott Hansen http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/#comment-25898 Wed, 16 Jan 2013 00:49:48 +0000 http://www.terminally-incoherent.com/blog/?p=13635#comment-25898

I found a nifty little trick that will only print the full prompt if you hit enter twice in a row or when opening a new terminal. Gives more room on the line for the rest of your commands.

.bashrc snippet here: http://sprunge.us/OQjU
git-completion-plus.sh: http://sprunge.us/SbRZ

This adds a nice indication as to what git branch you’re on, how many uncommitted changes and committed changes there are and the current path/hostname. The branch name and the number of changes all change color depending on the status. I like your idea of adding the IP address for remote servers. I only access a few different machines, so I just use different colors for my tmux status bar depending on which machine I’m logged in to (95% of the time I have tmux running).

firecat53@scotty:~/projects/company/inventory/src (master) 1 1 $

Scott

Reply  |  Quote
]]>