.bashrc and PS1 Environment

The combination of this .bashrc file and the PS1 variable makes my very useful PS1 prompt.

This config below will:

  • Make a very useful looking PS1 showing all adaptor IP addresses

  • PS1 will show date and time of command

  • Every command executed in bash will be logged to ~/.logs/.terminal_log/

.bashrc

alias ls="ls --color=auto"
alias ll="ls -l"
alias lll="ls -la"

RED=$(tput setaf 196)
GREEN=$(tput setaf 46)
YELLOW=$(tput setaf 226)
CYAN=$(tput setaf 117)
NORMAL=$(tput sgr0)

function build_ps1()
{
	t="$(which ifconfig)"
	if [ -z "$t" ]; then
		cmd="ip link show"
		cmd2="state UP"
	else
		cmd="ifconfig"
		cmd2="UP"
	fi

	cmd="$(which ifconfig)"
        PS1="[\[${CYAN}\]\t - \d\[${NORMAL}\]] "
        for i in $($cmd | grep $cmd2| awk '{print substr($1, 1, length($1)-1)}' | grep -v lo); do
                IP=$(ip addr show $i 2>/dev/null | grep inet | awk 'NR==1{print $2}')
                PS1="$PS1[$i:\[${YELLOW}\]$IP\[${NORMAL}\]]"
        done
        PS1="$PS1 [\[${GREEN}\]\u@\h\[${NORMAL}\]]\n[\[${RED}\]\w\[${NORMAL}\]] $ "
}

function history_log {

        export HISTTIMEFORMAT="%F-%T-%Z "
        if [ ! -d "~/.logs/.terminal_log" ]; then
                mkdir -p ~/.logs/.terminal_log
        fi
        test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f ~/.logs/.terminal_log/$(date "+%Z-%d-%m_%Y_%H-%M-%S")_"$$"_shell.log)
}

build_ps1
history_log

Last updated