OCI – 02a – Bash Prompt
Oct 06, 2023Dan Iverson
After creating a Git repository to track our first Terraform project, I updated my bash prompt to show me the git branch. This information is only shown when we are in Git repository and makes is easy to see which branch your working on.
This change is a bigger than just updating the PS1
variable, so I pulled all of the bash prompt code into a new file called ~/.bash_prompt
Here is the code for ~/.bash_prompt
$ cat ~/.bash_prompt
prompt_git() {
local s='';
local branchName='';
# Check if the current directory is in a Git repository.
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null;
# Check for uncommitted changes in the index.
if ! $(git diff --quiet --ignore-submodules --cached); then
s+='+';
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
# Check for stashed files.
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$';
fi;
fi;
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
[ -n "${s}" ] && s=" [${s}]";
echo -e "${1}${branchName}${2}${s}";
else
return;
fi;
}
export TERM=xterm-256color
bold='';
reset="\e[0m";
black="\e[1;30m";
blue="\e[1;34m";
cyan="\e[1;36m";
green="\e[38;5;64m";
orange="\e[38;5;166m";
purple="\e[1;35m";
red="\e[1;31m";
violet="\e[1;35m";
white="\e[1;37m";
yellow="\e[38;5;136m";
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
userStyle="${red}";
else
userStyle="${orange}";
fi;
# Set the terminal title and prompt.
PS1="\[\033]0;\W\007\]"; # working directory base name
PS1+="\[${bold}\]\n"; # newline
PS1+="\[${userStyle}\]\u"; # username
PS1+="\[${white}\] at ";
PS1+="\[${yellow}\]cloudshell(\$OCI_REGION)"; # host
PS1+="\[${white}\] in ";
PS1+="\[${green}\]\w"; # working directory full path
PS1+="\$(prompt_git \"\[${white}\] on \[${violet}\]\" \"\[${blue}\]\")"; # Git repository details
PS1+="\n";
PS1+="\[${white}\]\$ \[${reset}\]"; # `$` (and reset color)
export PS1;
PS2="\[${yellow}\]→ \[${reset}\]";
export PS2;
The next change is to remove the PS1
changes from ~/.bashrc
and instead source our new file.
# User specific aliases and functions
source /etc/bashrc.cloudshell
# Comment PS1 and load from .bash_prompt
#PS1=...
source ~/.bash_prompt
Finally, reload your new prompt. You can resource your entire .bashrc
file or just the prompt changes in .bash_prompt
$ source ~/.bash_prompt
Navigate to your Git repository and you should see the prompt displays the current branch.
Note: This was originally posted by Dan Iverson and has been transferred from a previous platform. There may be missing comments, style issues, and possibly broken links. If you have questions or comments, please contact [email protected].