Skip to content

Instantly share code, notes, and snippets.

@alexmurray
Last active July 26, 2023 05:51
Show Gist options
  • Save alexmurray/e7a5312a1798bd33234011e1f06cee04 to your computer and use it in GitHub Desktop.
Save alexmurray/e7a5312a1798bd33234011e1f06cee04 to your computer and use it in GitHub Desktop.
Show IP address in bash prompt
# Set the prompt to include the IP address instead of hostname
function get_ip () {
IFACE=$(ip -4 route | grep default | head -n1 | awk '{print $5}')
if [ ! -z $IFACE ]; then
echo -n "|"; ip -4 -o addr show scope global $IFACE | awk '{gsub(/\/.*/, "|",$4); print $4}' | paste -s -d ""
else
echo -n "||"
fi
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;34m\]@\[\033[32m\]$(get_ip)\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@$(get_ip):\w\$ '
fi
unset color_prompt force_color_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment