vote up 3 vote down star
5

What have you found to be useful in your .bash_profile ?

What tips do you have for people editing theirs ?


SEE ALSO: Useful BASH aliases ( ie: alias ...)

EDIT: I've removed my original .bash_profile added my current solution as an answer

flag
it might help to explain what does your .bash profile "contains"; I get alias commands, but first line is a mystery. – bbaja42 Nov 1 at 9:51
@bbaja42: PS1 is the environment variable that tells bash what your prompt should look like. the \033 stuff is for pretty colors. (well, ANSI colors, anyway.) – ~quack Nov 1 at 10:39
Ouch, you compile with -pedantic! – Martinho Fernandes Nov 1 at 10:58
2  
Non-printable things (such as escape sequences) should be wrapped between \[ and \], otherwise backspacing and line-wrapping will break. – grawity Nov 1 at 11:50

3 Answers

vote up 2 vote down

I don't have a ~/.bash_profile - all my customization is in ~/.bashrc (or files that it sources).

I find this prompt useful:

userprompt () {
local usercolor="\[\033[1;36m\]"
[[ $EUID == 0 ]] && usercolor="\[\033[1;31m\]"
PS1="$(pwd)"
PS1="$usercolor\u\[\033[0m\]@\[\033[1;34m\]\h\[\033[0m\]:${PS1//\//$usercolor/\[\033[0;1m\]}$usercolor\\$\[\033[0m\] "
}
PROMPT_COMMAND=userprompt

which sets the username, the slashes in the path and the prompt character ($ or #) to cyan for a regular user or red for root. It's done using PROMPT_COMMAND and a function so the color changes if you do sudo bash or su -m root.

This helps prevent accidents:

# must press ctrl-D 2+1 times to exit shell
export IGNOREEOF="2"

This helps with working with finding previously entered commands:

# allow ctrl-S for history navigation (with ctrl-R)
# today's terminal communication is too fast for ctrl-S/ctrl-Q anyway
stty -ixon

I like these as my default options for less:

# ignore case, long prompt, exit if it fits on one screen, allow colors for ls
# and grep colors, display search target at line 4, skip current screen for search
export LESS="-iMFXrj4a"
link|flag
vote up 1 vote down

Sleek, but context sensitive.

export PS1="\`
    if /bin/test -e \"\$(pwd)/.svn\"
    then
        echo \"\[^[[32m\]\u@\h:\w\[^[[0m\] (\[^[[1m^[[31m\]svn\[^[[0m\])\\$ \"
    elif /bin/test -e \"\$(pwd)/.bzr\"    then
        echo \"\[^[[32m\]\u@\h:\w\[^[[0m\] (\[^[[1m^[[32m\]bzr\[^[[0m\])\\$ \"
    else
        echo \"\[^[[32m\]\u@\h:\w\[^[[0m\] \$ \"    fi
\`"

And then this. Very useful

alias q='cd ..; ls'
link|flag
I love your Q! ;) So simple, yet, saves me a week every year... ;) – Aaaaaaaaaha ERLEBNIS Nov 3 at 11:04
Now combine it with highly nested directories, being a fast typist and a IBM model-M. yes, I can be scary. – Stefano Borini Nov 3 at 16:15
vote up 0 vote down

I'm now importing my .bashrc into my .bash_profile so I don't have to edit two files, it also ensures paths aren't modified multiple times:

# .bash_profile

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi


I've adopted Dennis' (cheers Dennis) approach to colorizing the LOGON relative to priviledges:

# .bashrc

shopt -s histverify

alias ?='history'  # DISPLAYS HISTORY

# JUMP TO DIRECTORIES WITHIN MY HOME DIRECTORY
alias 0='clear;cd ~/ARCHIVES/'
alias 1='clear;cd ~/AGENDA/'
alias 2='clear;cd ~/EVENTS/'
alias 3='clear;cd ~/CORRESPONDENCE/'
alias 4='clear;cd ~/CONTACTS/'
alias 5='clear;cd ~/TEXTS/'
alias 6='clear;cd ~/IMAGES/'
alias 7='clear;cd ~/VIDEO/'
alias 8='clear;cd ~/AUDIO/'
alias 9='clear;cd ~/TRANSACTIONS/'
alias X='clear;cd ~/CODE/'

alias c='clear'  # CLEARSCREEN

alias COMPILE='gcc -W -Wall -ansi -pedantic -O3 -o'

alias EB='nano ~/.bashrc'
alias EBP='nano ~/.bash_profile'

alias h='clear; cd ~; ls -F -G'  # GOTO HOME & LIST CONTENTS

alias l='clear; ls -F -G -h -l'
alias la='clear; ls -F -G -a'
alias ll='clear; ls -@ -A -F -G -h -l'

alias p='clear; ps -a'
alias pa='clear; ps -A -a'
alias pl='clear; ps -A -l'

alias r='clear; cd /; ls -F -G'  # GOTO ROOT & LIST CONTENTS

alias u='clear; cd ..; ls -F -G'  # GO UP ONE LEVEL & LIST CONTENTS

fn_prompt_command() {

    local default="\[\033[0m\]"
    local green="\[\033[0;32m\]"
    local red="\[\033[1;31m\]"

    PS1="\[\n\]"
    PS1+="$green TIMESTAMP ::$default \[\@ \d\n\]"
    [[ $EUID == 0 ]] && PS1+="$red     LOGON :: \[\\u@\\H\n\]"  # ROOT PROMPT
    [[ $EUID != 0 ]] && PS1+="$green     LOGON ::$default \[\\u@\\H\n\]"  # DEFAULT PROMPT   
    PS1+="$green  LOCATION ::$default \[\\w\n\]"
    PS1+="$green  ARGUMENT ::$default\[ \]"
}

PROMPT_COMMAND=fn_prompt_command

export PATH="/opt/local/bin:/opt/local/sbin:${PATH}"

export MANPATH="/opt/local/share/man:${MANPATH}"
link|flag
You know that you can clear the screen with Ctrl-L, right? – Teddy Nov 3 at 13:03
@Teddy: yup, it's just a preference. – _ande_turner_ Nov 5 at 4:35

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.