You are not logged in.

#1 2006-05-17 22:09:43

hollywoodb
Member
From: USA
Registered: 2003-10-03
Posts: 39

basic ~/.bashrc ~/.bash_profile tips thread

A lot of this is borrowed from other sites, including the wiki
This by no means all-inclusive, please add on anything interesting along this topic wink

About ~/.bash_profile and ~/.bashrc

disclaimer:  I don't use ~/.bash_profile or ~/.profile
there is plenty of documentation on the web on how to use these properly if you're interested

in fact, my ~/.bash_profile looks like this:

#!/bin/sh

### THIS IS A DUMMY FILE
### DON'T NEED TWO FILES TO DO ONE JOB
### LET'S CALL ~/.bashrc INSTEAD

source ~/.bashrc

Now, if you choose to go this method, there's a little trick you can do to eliminate ~/.bash_profile altogether  :twisted:

as root create file /etc/profile.d/bashrc.sh with contents:

#!/bin/sh

source ~/.bashrc

AND

chmod +x /etc/profile.d/bashrc.sh

Since /etc/profile executes everything in /etc/profile.d/ that is chmod executable, the little script we just created will cause ~/.bashrc to be read on every login... I personally prefer this method to the dummy ~/.bash_profile, but I keep both since I share my /home directory across distributions and not all distros are the same.


Now, on to the actual ~/.bashrc:

#!/bin/sh


### ALIASES

alias ls="ls --color"
alias ll="ls --color -lh"
alias la="ls --color -a"
alias pacs="pacsearch"
alias vim="vim -y"
alias nano="nano -w"





### NICE COLOR BASH PROMPT

function bash_prompt

{
local WHITE="[33[1;37m]"
local default="[33[0;39m]"
local BRIGHTGREEN="[33[1;32m]"
local GREEN="[33[0;32m]"
local CYAN="[33[0;36m]"
local GRAY="[33[0;37m]"
local RED="[33[0;31m]"

if [ `id -u` != "0" ]; then
   PS1="${GREEN}u${CYAN}@${GREEN}h ${CYAN}w${WHITE} ${default}$ "
else
   PS1="${RED}u${CYAN}@${GREEN}h ${CYAN}w${WHITE} ${default}$ "
fi
}

bash_prompt





### COLORIZE PACMAN (PACS)

pacsearch () {
       echo -e "$(pacman -Ss $@ | sed 
       -e 's#current/.*#\033[1;31m&\033[0;37m#g' 
       -e 's#extra/.*#\033[0;32m&\033[0;37m#g' 
       -e 's#community/.*#\033[1;35m&\033[0;37m#g' 
       -e 's#^.*/.* [0-9].*#\033[0;36m&\033[0;37m#g' )"
}

This is really pretty basic.  I use the same ~/.bashrc for all users, including root...

The alias section is pretty self explanatory, basically the parts in quotations (" ") get run instead of the commands before the equals (=) in every case.

The bash prompt section is a bit nifty.  The first part, with color definitions, is so that I don't go mad trying to write the actual PS1 line.  Makes it cleaner in my opinion.  There's lots of documentation out there if you're interested in modifying the prompt I have.

The if/then/else statement in the bash prompt section checks if the user is root.  If not, the username is displayed in GREEN at the prompt.  If the user is root, the username is displayed in RED.  A nice visual reminder, and since I use my ~/.bashrc for ALL users, including root, it helps make the file more portable.

The last section is taken straight from the Arch wiki.  At the top I aliased 'pacs', which calls this section.  Basically 'pacs' can take the place of 'pacman -Ss' and offer simple colour output.

http://wiki.archlinux.org/index.php/Col … man_output

Additions, other bash tips & tricks, comments, all welcome

Offline

#2 2006-05-18 03:29:15

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

hollywoodb wrote:

in fact, my ~/.bash_profile looks like this:

#!/bin/sh

### THIS IS A DUMMY FILE
### DON'T NEED TWO FILES TO DO ONE JOB
### LET'S CALL ~/.bashrc INSTEAD

source ~/.bashrc

I prefer to symlink bash_profile to bashrc

Offline

#3 2006-05-18 07:59:03

FUBAR
Member
From: Belgium
Registered: 2004-12-08
Posts: 1,029
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

I need to be in this thread!

*Find back post*


A bus station is where a bus stops.
A train station is where a train stops.
On my desk I have a workstation.

Offline

#4 2006-05-18 22:20:13

jakob
Member
From: Berlin
Registered: 2005-10-27
Posts: 419

Re: basic ~/.bashrc ~/.bash_profile tips thread

Is it allowed to paste stuff that was copied from other members of this forum?

I found this one on rezza's configs. It makes extraction of compressed files easier. Of course to be put in .bashrc

ex () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xjf $1        ;;
            *.tar.gz)    tar xzf $1     ;;
            *.bz2)       bunzip2 $1       ;;
            *.rar)       rar x $1     ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xf $1        ;;
            *.tbz2)      tar xjf $1      ;;
            *.tgz)       tar xzf $1       ;;
            *.zip)       unzip $1     ;;
            *.Z)         uncompress $1  ;;
            *.7z)        7z x $1    ;;
            *)           echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

Again: this one is not from me, but rezza!!

Offline

#5 2006-05-18 22:43:39

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

In addition to that same function ganked from rezza, I have a handful of little functions in my bashrc:
(note: hex2dec and dec2hex removed because they broke... I'll fix them later)

function calc() { echo "$*" | bc; }
function mktar() { tar czf "${1%%/}.tar.gz" "${1%%/}/"; }
function mkmine() { sudo chown -R ${USER} ${1:-.}; }

function newmail()
{
    tot=0
    for mb in $MAIL/*; do
        cnt=`ls -1 $mb/new/|wc -l`
        tot=`expr $tot + $cnt`
        echo "$(basename $mb):$cnt"
    done
    echo "Total:$tot"
}

This one is my favorite... i use vim alot, so I find myself trying to use vim commands in the bash shell sometimes:

:h() {  vim --cmd ":silent help $@" --cmd "only"; }

I also used to have:

:q() { exit }

Offline

#6 2006-05-19 00:47:09

palandir
Member
Registered: 2006-05-14
Posts: 73

Re: basic ~/.bashrc ~/.bash_profile tips thread

I added some comments explaining each piece.

Misc stuff:

# My prompt, quite basic, decent coloring, shows the value of $?
# (exit value of last command, useful sometimes):
C_DEFAULT="[33[0m]"
C_BLUE="[33[0;34m]"
export PS1="$C_BLUE($C_DEFAULT$?$C_BLUE)[$C_DEFAULTu$C_BLUE@$C_DEFAULTh$C_BLUE:$C_DEFAULTw$C_BLUE]\$ $C_DEFAULT"
export PS2="$C_BLUE> $C_DEFAULT"

# If you allow Ctrl+Alt+Backspace to kill the X server but are paranoid,
# then this alias will ensure that there will be no shell open afterwards.
alias startx="exec startx"

# Let grep colorize the search results
alias g="egrep --color=always"
alias gi="egrep -i --color=always"

# Hostname appended to bash history filename
export HISTFILE="$HOME/.bash_history_`hostname -s`"

# Don't save repeated commands in bash history
export HISTCONTROL="ignoredups"

# Confirm before overwriting something
alias cp="cp -i"

# Disable ^S/^Q flow control (does anyone like/use this at all?)
stty -ixon

# If your resolution gets fucked up, use this to reset (requires XRandR)
alias resreset="xrandr --size 1280x1024"

And some small but handy functions:

# mkmv - creates a new directory and moves the file into it, in 1 step
# Usage: mkmv <file> <directory>
mkmv() {
    mkdir "$2"
    mv "$1" "$2"
}

# sanitize - set file/directory owner and permissions to normal values (644/755)
# Usage: sanitize <file>
sanitize() {
    chmod -R u=rwX,go=rX "$@"
    chown -R ${USER}.users "$@"
}

# nh - run command detached from terminal and without output
# Usage: nh <command>
nh() {
    nohup "$@" &>/dev/null &
}

# run - compile a simple c or cpp file, run the program, afterwards delete it
# Usage: run <file> [params]
run() {
    filename="${1%%.*}"
    extension="${1##*.}"
    file="$1"
    shift
    params="$@"
    command=""

    if [ $extension = "cc" -o $extension = "cpp" -o $extension = "c++" ]; then
        command="g++"
    elif [ $extension = "c" ]; then
        command="gcc"
    else
        echo "Invalid file extension!"
        return 1
    fi 

    $command -Wall -o $filename $file
    chmod a+x $filename
    ./$filename $params
    rm -f $filename 2>/dev/null
}

Offline

#7 2006-05-19 02:15:54

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

palandir wrote:
# Disable ^S/^Q flow control (does anyone like/use this at all?)
stty -ixon

Awesome! Thanks

Offline

#8 2006-05-19 10:54:14

nightfrost
Member
From: Sweden
Registered: 2005-04-16
Posts: 647

Re: basic ~/.bashrc ~/.bash_profile tips thread

FUBAR wrote:

I need to be in this thread!

*Find back post*

yeah me too smile

Offline

#9 2006-05-23 08:56:20

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

phrakture wrote:
palandir wrote:
# Disable ^S/^Q flow control (does anyone like/use this at all?)
stty -ixon

Awesome! Thanks

What the smeg does that mean?  *feels left out*


.oO Komodo Dave Oo.

Offline

#10 2006-05-23 11:40:49

postlogic
Member
Registered: 2005-02-24
Posts: 410
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

The only useful thing I have is this:

alias pacman="sudo pacman"

It's simple. It lacks teh bling. But I'm tired of typing sudo pacman.

Offline

#11 2006-05-23 12:56:12

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,838

Re: basic ~/.bashrc ~/.bash_profile tips thread

Nothing fancy here either, just a bunch of aliases to save keystrokes:

alias swanstart='sudo /etc/rc.d/openswan start'
alias swanrestart='sudo /etc/rc.d/openswan restart'
alias swanstop='sudo /etc/rc.d/openswan stop'           
alias swanstatus='sudo /etc/rc.d/openswan status'
alias nfs='sudo /etc/rc.d/netfs'
alias pg='ps ax | grep'
alias pss='pacman -Ss'
alias pq='pacman -Q | grep'
alias pi='pacman -Qi'
alias pl='pacman -Ql'
alias po='pacman -Qo'
alias pacman='sudo pacman'
alias bear='sudo /usr/sbin/hibernate'
alias eject='eject /cdrom'
alias tarpkg='tar czf `pwd | cut -d "/" -f 5`.tar.gz PKGBUILD'
alias mymp='mplayer -fs -stop-xscreensaver -cache 8192'
Komodo wrote:

What the smeg does that mean?  *feels left out*

Me too - &%$£ing geeks ........:P

Offline

#12 2006-05-23 14:23:35

Romashka
Forum Fellow
Registered: 2005-12-07
Posts: 1,054

Re: basic ~/.bashrc ~/.bash_profile tips thread

IMHO this thread should be wikified. smile


to live is to die

Offline

#13 2006-05-23 14:30:36

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

Komodo wrote:
phrakture wrote:
palandir wrote:
# Disable ^S/^Q flow control (does anyone like/use this at all?)
stty -ixon

Awesome! Thanks

What the smeg does that mean?  *feels left out*

Yay nested quotes!

Try hitting CTRL-S in a bash term, type a bunch of stuff, then hit CTRL-Q; you'll see what it does.

Offline

#14 2006-05-23 14:31:34

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: basic ~/.bashrc ~/.bash_profile tips thread

Romashka wrote:

IMHO this thread should be wikified. smile

Then add it to the wiki!  Youu... haavveee.... the POWAAA!  (lame He-Man reference)

Offline

#15 2006-05-24 01:43:47

jakob
Member
From: Berlin
Registered: 2005-10-27
Posts: 419

Re: basic ~/.bashrc ~/.bash_profile tips thread

Where we are at pacman aliases

#Pacman shortcuts
alias p="pacman" 
alias pQ="pacman -Q"
alias pqs="pacman -Qs" 
alias pqi="pacman -Qi"
alias pS="sudo pacman -S"
alias psyu="sudo pacman -Syu"
alias psi="pacman -Si"
alias pR="sudo pacman -R"
alias prc="sudo pacman -Rc"
alias prs="sudo pacman -Rs"
alias prsc="sudo pacman -Rsc"
alias pA="sudo pacman -A"
alias pU="sudo pacman -U"
alias pO="sudo pacman-optimize"
#Colorful pacman -Ss output
pss () {
       echo -e "$(pacman -Ss $@ | sed 
       -e 's#current/.*#\033[1;31m&\033[0;37m#g' 
       -e 's#extra/.*#\033[0;32m&\033[0;37m#g' 
       -e 's#community/.*#\033[1;35m&\033[0;37m#g' 
       -e 's#^.*/.* [0-9].*#\033[0;36m&\033[0;37m#g' )"
}

scnr

Offline

#16 2006-05-24 08:14:58

Purch
Member
From: Finland
Registered: 2006-02-23
Posts: 229

Re: basic ~/.bashrc ~/.bash_profile tips thread

#Colorful pacman -Ss output
pss () {
       echo -e "$(pacman -Ss $@ | sed 
       -e 's#current/.*#\033[1;31m&\033[0;37m#g' 
       -e 's#extra/.*#\033[0;32m&\033[0;37m#g' 
       -e 's#community/.*#\033[1;35m&\033[0;37m#g' 
       -e 's#^.*/.* [0-9].*#\033[0;36m&\033[0;37m#g' )"
}
$ pss xfce4-svn
-bash: -e: command not found
-bash: -e: command not found
-bash: -e: command not found
-bash: -e: command not found
xfce-svn/xfce4-pluginpack-svn 0.0.5-1
    Xfce4 panel plugins, ported to Xfce4-svn panel

Ok, who broke my bash and sed?

Offline

#17 2006-05-24 13:12:08

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

Cerebral wrote:

Try hitting CTRL-S in a bash term, type a bunch of stuff, then hit CTRL-Q; you'll see what it does.

Ah, I knew that actually... I do it all the time -_-  It's well irritating, so that tidy bash nibblet will be mighty useful big_smile


.oO Komodo Dave Oo.

Offline

#18 2006-05-24 13:14:14

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

aman wrote:
#Colorful pacman -Ss output
pss () {
       echo -e "$(pacman -Ss $@ | sed 
       -e 's#current/.*#\033[1;31m&\033[0;37m#g' 
       -e 's#extra/.*#\033[0;32m&\033[0;37m#g' 
       -e 's#community/.*#\033[1;35m&\033[0;37m#g' 
       -e 's#^.*/.* [0-9].*#\033[0;36m&\033[0;37m#g' )"
}

Ok, who broke my bash and sed?

pacsearch () {
         echo -e "$(pacman -Ss $@ | sed 
        -e 's#current/.*#\033[0;31m&\033[0;37m#g' 
        -e 's#extra/.*#\033[0;32m&\033[0;37m#g' 
        -e 's#community/.*#\033[0;35m&\033[0;37m#g' 
        -e 's#^.*/.* .*#\033[0;36m&\033[0;37m#g')e[m"

..spot the difference smile


.oO Komodo Dave Oo.

Offline

#19 2006-05-24 13:54:45

Purch
Member
From: Finland
Registered: 2006-02-23
Posts: 229

Re: basic ~/.bashrc ~/.bash_profile tips thread

no spotting sad
... and the "pss" vs "pacsearch" is not the solution.

Offline

#20 2006-05-24 14:16:26

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

aman wrote:

no spotting sad
... and the "pss" vs "pacsearch" is not the solution.

lol!  I didn't see a difference tbh, i just thought if you tried the one rezza has, it might work tongue  unlucky man :shock:


.oO Komodo Dave Oo.

Offline

#21 2006-05-24 22:17:37

sh__
Member
Registered: 2005-07-19
Posts: 271

Re: basic ~/.bashrc ~/.bash_profile tips thread

aman wrote:

no spotting sad
... and the "pss" vs "pacsearch" is not the solution.

Might be a problem with copy&paste. Check that there are no spaces after '' at the end of every line that seems to end with a ''.

Offline

#22 2006-05-24 23:55:38

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

It would be handy if someone wrote a couple of functions for renaming things...

You could have 'rename [current file extension] [desired file extension]' which would perform

`ls -d | sed 's:(.*)$1:mv "&" "1$2"' | sh`

...or _possibly_ a similar command that removes any completely intentional errors I've included in there tongue  (it's late, I'm tired, so shoot me -_-).


.oO Komodo Dave Oo.

Offline

#23 2006-05-25 08:11:52

Purch
Member
From: Finland
Registered: 2006-02-23
Posts: 229

Re: basic ~/.bashrc ~/.bash_profile tips thread

sh__ wrote:

Check that there are no spaces after '' at the end of every line

Ups :oops: Thanks sh__ you fixed my bash and sed wink

Offline

#24 2006-05-26 16:02:51

palandir
Member
Registered: 2006-05-14
Posts: 73

Re: basic ~/.bashrc ~/.bash_profile tips thread

Komodo wrote:

It would be handy if someone wrote a couple of functions for renaming things...

You could have 'rename [current file extension] [desired file extension]' which would perform

`ls -d | sed 's:(.*)$1:mv "&" "1$2"' | sh`

...or _possibly_ a similar command that removes any completely intentional errors I've included in there tongue  (it's late, I'm tired, so shoot me -_-).

If you know Perl's regular expressions and the s/// function, then you can also use rename. I use it quite often, it's really great, and it's a bit easier to use than forming long bash one-liners with sed.
For example, to lower-case everything, type:

rename -c *

And your example above could be done with:

rename 's/.ext$/.newext/' file.ext

If you do the same thing often you can also alias that.
There are more examples on the page or in the perldoc.
Another cool thing is that you aren't limited to s///, you can use any Perl code, and as many instructions separated by ';' you like, because it gets eval()ed.
And, of course, there's a preview option.

Offline

#25 2006-05-27 05:28:33

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: basic ~/.bashrc ~/.bash_profile tips thread

palandir wrote:

If you know Perl's regular expressions and the s/// function, then you can also use rename. I use it quite often, it's really great, and it's a bit easier to use than forming long bash one-liners with sed.

Giggidy, useful info there, cheers palandir wink


.oO Komodo Dave Oo.

Offline

Board footer

Powered by FluxBB