Skip to content

Instantly share code, notes, and snippets.

@rickumali
Last active May 7, 2023 03:46
Show Gist options
  • Save rickumali/92e4c5d8e28cb8078ca4d52b26b0a016 to your computer and use it in GitHub Desktop.
Save rickumali/92e4c5d8e28cb8078ca4d52b26b0a016 to your computer and use it in GitHub Desktop.
Examining BASH PROMPT_COMMAND and BASH Prompt Colors

README-bash-prompt

This Gist supports this YouTube video:

https://www.youtube.com/watch?v=rnZYaQ7BfT8

from the YouTube playlist "Rick is Playing With Unix"

PROMPT_COMMAND

The scripts prompt1.sh through prompt7.sh walk through an examination of the PROMPT_COMMAND variable. To exercise these, do 'source' on each file.

Prompt Colors

The prompt-04-color-test scripts (3) walk through a few approaches for setting the color in a BASH prompt. To exercise these, do 'source' on each file.

From my experimentation, escape sequences seems to work the best!

# Set prompt colors using ansi tool
# https://github.com/fidian/ansi
pc4_pwd ()
{
ansi -n --bold --green "rick@raspberrypi"
ansi -n --reset-color
ansi -n ":"
ansi -n --bold --blue `pwd`
ansi -n --reset-color
}
PROMPT_COMMAND=pc4_pwd
PS1=" % "
export PS1
# Set prompt colors using escape sequences
# See https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
ESC='\033'
RESET='0'
BOLD='1'
GREEN='32'
BLUE='34'
PS1="\[${ESC}[${BOLD};${GREEN}m\]\u@\h\[${ESC}[${RESET}m\]:\[${ESC}[${BOLD};${BLUE}m\]\w \$\[${ESC}[${RESET}m\] "
export PS1
# Set prompt colors using tput
# https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
ESC=''
RESET=$(tput sgr0)
BOLD=$(tput bold)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
PS1="${BOLD}${GREEN}\u@\h${RESET}:${BOLD}${BLUE}\w \$${RESET} "
export PS1
PROMPT_COMMAND="touch pc1-`date +%T`"
PS1='TEST1 % '
# Every time you display prompt, you run PROMPT_COMMAND
# PROBLEM: The filename doesn't ever change
pc2_touch ()
{
touch pc2-`date +%T`
}
PROMPT_COMMAND="pc2_touch"
PS1='TEST2 % '
# This fixes the problem from prompt1.sh
# You'll see MANY pc2 files as you enter commands
pc3_touch ()
{
touch pc3-`date +%T`
}
# uname -r = Release
# uname -s = System
PROMPT_COMMAND=("pc3_touch" "uname -r" "uname -s")
PS1='TEST3 % '
# This demonstrates that PROMPT_COMMAND can be an array
# Each element in the array is executed
pc4_touch ()
{
touch pc4-`date +%T`
}
# uname -r = Release
# uname -s = System
PROMPT_COMMAND=("pc4_touch" "val1=$(uname -r)" "val2=$(uname -s)")
PS1='TEST4 ($val1) ($val2) % '
# This variation on prompt3 displays the output of the
# commands into a single line
pc5_date ()
{
echo `date +%T`
}
pc5_num ()
{
echo $RANDOM
}
PROMPT_COMMAND=("val1=$(pc5_date)" "val2=$(pc5_num)" "val3=$RANDOM")
PS1='TEST5 ($val1) ($val2) ($val3) % '
# This attempts to set and display a variable in the prompt
# to some dynamic output
# PROBLEM: val1, val2, and val3 will remain fixed
shopt -u promptvars
pc6_num ()
{
echo $RANDOM
}
PROMPT_COMMAND=("val1=$(date +%T)" "val2=$(pc6_num)" val3=$RANDOM)
PS1='TEST6 ($val1) ($val2) ($val3) % '
# This attempts to fix prompt5 by disabling promptvars
# PROBLEM: It still doesn't work. Worse... you have to re-enable
# promptvars with shopt -s promptvars
pc7_date ()
{
val1=$(date +%T)
}
pc7_num ()
{
val2=${RANDOM}
}
PROMPT_COMMAND=(pc7_date pc7_num val3=$RANDOM)
PS1='TEST7 ($val1) ($val2) ($val3) % '
# This fixes the problems from prompt5 and prompt6
# using global variables (except for val3, which uses
# the incorrect method).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment