Bash

From Bit Binary Wiki
Jump to: navigation, search

Contents

Bash configuration

Below you will find current bash startup files below. for more information see Bash Startup Files.

.profile

If you use this ~/.profile conf file you should also be using ~/bin as you users local bin folder.

mkdir ~/bin
# ~/.profile: Executed by dash for login shells. Syntax must be dash
# compatible!
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
umask 027

# if running bash
if [ -n "$BASH_VERSION" ] ; then
    # include .bashrc if it exists
    if [ -f "${HOME}/.bashrc" ] ; then
	    . "${HOME}/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "${HOME}/bin" ] ; then
    PATH="${HOME}/bin:$PATH"
fi

# Include sbin paths for sudo users
if groups | grep sudo > /dev/null 2>&1 ; then 
    PATH="/usr/local/sbin:/usr/sbin:/sbin:${PATH}"
fi

# Disallow write access to terminal
mesg n

if [ -x /usr/bin/startx ] && [ "${STARTX}" == "true" ] ; then 
    if [ -z ${DISPLAY} ] && [ $(tty) = /dev/tty1 ] ; then
        #{ exec startx -- -dpi 96 2>&1 | logger -t x-session ; exit ; }
        { exec startx 2>&1 | logger -t x-session ; exit ; }
    fi
fi

.bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# See /usr/share/doc/bash/examples/startup-files in the package bash-doc
# for examples.

# Start X automatically after login to TTY1?
STARTX=false

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return ;;
esac

# Aliases
[ -f ${HOME}/.bash_aliases ] && . ${HOME}/.bash_aliases

# Functions
[ -f ${HOME}/.bash_functions ] && . ${HOME}/.bash_functions

# See bash(1) for info on the following shell options and variables.
shopt -s autocd
shopt -s cdspell
shopt -s checkwinsize
shopt -s cmdhist
shopt -s direxpand
shopt -s globstar
shopt -s histappend
HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
HISTCONTROL=ignoreboth:erasedups
HISTSIZE=10000
HISTFILESIZE=20000

# Make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# Give man pages some colour
export LESS_TERMCAP_mb=$'\e[1;34m'
export LESS_TERMCAP_md=$'\e[1;34m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[1;44;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;32m'

# Default editor (can be set via update-alternatives)
export EDITOR=vim

# Ensure uniform QT and GTK (use qtconfig to select GTK)
if [ -f ${HOME}/.gtkrc-2.0 ] ; then
    export GTK2_RC_FILES="${HOME}/.gtkrc-2.0"
fi

# Enable bash completion in interactive shells
if ! shopt -oq posix ; then
    if [ -f /usr/share/bash-completion/bash_completion ] ; then
        . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ] ; then
        . /etc/bash_completion
    fi
fi

# set X terminal window titles and write history after each command.
case ${TERM} in
    xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
        ;;
    screen)
        PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\033\\"'
        ;;
esac
PROMPT_COMMAND="${PROMPT_COMMAND:-:} ; history -a"
export PROMPT_COMMAND

# Command Prompt
if [ "$(id -u)" -eq "0" ] ; then
    export PS1='\[\e[0;31;40m\]\u\[\e[1;30m\]@\h \[\e[36m\]\W \[\e[32m\]>\[\e[0m\] '
else
    export PS1='\[\e[0;32;40m\]\u\[\e[1;30m\]@\h \[\e[36m\]\W \[\e[32m\]>\[\e[0m\] '
fi

.bash_functions

# Functions

# Hold a package
apt-hold()
{
    for i in "${@}" ; do
        echo "${i} hold" | sudo dpkg --set-selections
        echo "\"${i}\" set to hold"
    done
}

# Remove package from hold
apt-unhold()
{
    for i in "${@}" ; do
        echo "${i} install" | sudo dpkg --set-selections
        echo "\"${i}\" set to install"
    done
}

# Show held packages
apt-held() { dpkg --get-selections | grep hold ; }

# Backup a file
bu() { cp ${1} ${1}-$(date +%Y%m%d%H%M%S).backup ; }

# Print dir after cd
cd() { builtin cd "${@}" && echo ${PWD// /\\ } ; }

# Create tar.gz of directory
mktar() { tar czf "${1%%/}.tar.gz" "${1%%/}/" ; }

# Restart network interfaces specified as arguments
netrestart()
{
    for iface in "${@}" ; do
        nohup sh -c "sudo ifdown ${iface} && sudo ifup ${iface}"
    done
}

# Public rdns
pubhost() { curl ifconfig.me/host ; }

# Public IP
pubip() { curl ifconfig.me ; }

.bash_aliases

# Aliases

alias sudo='sudo '                      # Ensure aliases work with sudo
alias rm='rm -i'                        # Double check rm
alias cp='cp -i'                        # Double check cp
alias mv='mv -i'                        # Double check mv
alias hist='history | grep ${1}'        # Quick history search
alias aptitude='apt-get'                # Avoid aptitude usage
alias apt-why='/usr/bin/aptitude why'   # Why package is to be installed
alias apt-listbp='dpkg -l | awk "/^ii/ && \$3 ~ /bpo[4567]0/ {print \$2}"'
alias ..='cd ..'                        # Up one directory
alias ...='cd ../..'                    # Up two directories
alias ....='cd ../../..'                # UP three directories
alias extract='unp'                     # use unp for extract
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'

# Use default dircolors with ls and grep
if [ -x /usr/bin/dircolors ] ; then
    [ -r ${HOME}/.dircolors ] \
    && eval "$(dircolors -b ${HOME}/.dircolors)" \
    || eval "$(dircolors -b)"
    alias ls='ls -CF --color=auto'
    alias l='ls -lh --color=auto'
    alias ll='ls -lah --color=auto'
    alias grep='grep --color=auto'
    alias igrep='grep -i --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

.bash_logout

Note: This is the default on Debian Wheezy so usually isn't required.
# ~/.bash_logout: executed by bash(1) when login shell exits. 

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

Skel

If you want to use custom bash startup files for all users then you can place them in /etc/skel so new user accounts use them. for existing user accounts you can run this command to update them as the user.

cp -R /etc/skel/. ~/

References

How can I avoid losing any history lines?

Bash tips: Colors and formatting

Color Bash Prompt

Using ANSI Color Codes to Colorize Your Bash Prompt on Linux

Terminals Can Have Colors!

Tip: Prompt magic

Personal tools