<h1 align="center">
<a href="https://prompts.chat">
A simple _Bash_ prompt customizer that displays useful information in your _Bash_ prompt, such as: device hostname,
Sign in to like and favorite skills
A simple Bash prompt customizer that displays useful information in your Bash prompt, such as: device hostname, operating system, git repository status, etc. and which can be extended and customized.
The goal of this project is to have a single Bash script that customizes the prompt and to avoid all the overheads of some more complex utilities.
The script is written almost only with Bash built-in commands and try to use as little sub-shells as possible, in order to keep it simple and fast.
4.2git to be installed and works only with a version >= 2.11Currently 5 segments are available:
.git directoryThe font and background colors of each segment is customizable, just as their display order.
A last non-customizable segment is displayed on a new line, it contains a dollar sign (
$).
If ever the last ran Bash command returned an error code, the dollar sign will be shown in red.
The terminal title may be setup with the
PBP_CONFIG_TERM_TITLE, for example:
PBP_CONFIG_TERM_TITLE='\u@\h'
Segments order and colors may be setup with the
PBP_CONFIG_SEGMENTS global variable.
This variable is an array containing the specification of each segment to be displayed.
The specification syntax consists of 4 parameters separated by a slash (
/):
segment_name/segment_background_color/segment_font_color/segment_prefix
For example:
hostname/8;2;44;0;33/8;5;251/❄
The element of the array defines the display order of the segments. For example, the default value is:
PBP_CONFIG_SEGMENTS=( "hostname/8;2;32;45;47/8;5;250" "separator/8;2;77;64;52/8;2;32;45;47/▛" "separator/8;2;32;45;47/8;2;77;64;52/▟" "os/8;2;77;64;52/8;5;250" "separator/8;2;37;41;52/8;2;77;64;52/▛" "separator/8;2;77;64;52/8;2;37;41;52/▟" "path/8;2;37;41;52/8;5;250" "separator/8;2;40;59;43/8;2;37;41;52/▛" "separator/8;2;37;41;52/8;2;40;59;43/▟" "git/8;2;40;59;43/8;5;250/⑂" )
By default, the script prevent the segments to end with a separator segment. This behavior may be cancelled by setting the
PBP_CONFIG_SEGMENTS global
variable to 0. The default value 1 leaves the trailing separator segments
intact:
PBP_CONFIG_CHOP_LAST_SEPARATOR=0
The git segment displays 3 main information about the current repository:
The symbol of each of those information may be setup with the
PBP_CONFIG_GIT_SYMBOLS global associative array
variable.
For example, the default value is:
PBP_CONFIG_GIT_SYMBOLS=( [dirty]="*" [ahead]="⭫" [behind]="⭭" )
In order to test the Bash prompt script, copy the
powerline-bash-prompt.bash file, then simply source it in the
terminal and set the PROMPT_COMMAND Bash variable:
$ . powerline-bash-prompt.bash $ PROMPT_COMMAND='__pbp_generate_ps1 $?'
Copy and store the
powerline-bash-prompt.bash file somewhere like your $HOME directory.
Then optionally configure the PBP_CONFIG_* variables, source the script in your .bashrc and execute the
__pbp_register_prompt_command function:
# Check if the script exists or fallback to a safe prompt value if [ -f ~/powerline-bash-prompt.bash ] then declare -a PBP_CONFIG_SEGMENTS=( "hostname/8;2;32;45;47/8;5;250" "path/8;2;37;41;52/8;5;250" "git/8;2;40;59;43/8;5;250/⑂" ) declare -A PBP_CONFIG_GIT_SYMBOLS=( [dirty]="*" [ahead]="+" [behind]="-" ) . ~/powerline-bash-prompt.bash __pbp_register_prompt_command else PS1='\u@\h\:\w\$ ' fi
Bash might be reloaded with the
exec command to apply the prompt customization:
$ exec $SHELL
In order create a new segment the only requirements is to declare a new function based on the following syntax:
_pbp_get_NAME_segment where NAME is the name
of the new segment.
This new function will receive 3 parameters:
$1 the segment font color ANSI sequence$2 the segment background color ANSI sequence$3 optionally the segment prefixThen it must set the following key of the global
__pbp_segment_struct
associative array:
bg_colorfg_colorcontentsHere is an example with a segment named
dummy:
__pbp_get_dummy_segment() { local contents="" if [ -n "${3}" ]; then contents+=" ${3}" fi contents+=' I am a dummy segment' __pbp_segment_struct[bg_color]="${1}"; __pbp_segment_struct[fg_color]="${2}" __pbp_segment_struct[contents]="${contents}" }
Then the
dummy segment might be rendered by calling it in the
PBP_CONFIG_SEGMENTS global variable:
declare -a PBP_CONFIG_SEGMENTS=( "hostname/8;2;32;45;47/8;5;250" "dummy/8;2;37;41;52/8;5;250" )