Alek's Blog

bash settings

Updated:
Table of Contents

You will find here some all day work setting for bash.

PS1🔗

Sometimes it it helpful to know when which command was executed. The below PS1 shows the date timestamp with timezone

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] on \D{%d/%m/%Y} at \D{%H:%M:%S_%Z} \[\033[01;34m\]\w\[\033[00m\] \$\n# '

This is an example output.

alex@alex-tuxedoinfinitybooks1517gen7 on 24/08/2025 at 19:12:45_CEST /datadisk/git-repos/zola-learn/none-blog $
# zola serve

History🔗

This snippet creates the file ~/.persistent_history for longer history of the bash.

# ~/.bashrc: executed by bash(1) for non-login shells.

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoredups:ignorespace:erasedups

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# some other stuff

log_bash_persistent_history() {
  [[
    $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
  ]]
  local date_part="${BASH_REMATCH[1]}"
  local command_part="${BASH_REMATCH[2]}"
  if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
  then
    echo $date_part "|" "$command_part" >> ~/.persistent_history
    export PERSISTENT_HISTORY_LAST="$command_part"
  fi
}

# Stuff to do on PROMPT_COMMAND
run_on_prompt_command(){
  log_bash_persistent_history
  history -a
  history -c
  history -r

  local pwd='~'
  [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
  pwd="${pwd//[[:cntrl:]]}"
  printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${pwd}"
}

HISTTIMEFORMAT="%d/%m/%y %T "
PROMPT_COMMAND="run_on_prompt_command"