Skip to content

Instantly share code, notes, and snippets.

@pocco81
Forked from alexmurray/.bashrc
Created November 20, 2021 23:49
Show Gist options
  • Save pocco81/98533b157790eaa6219864db1694d6db to your computer and use it in GitHub Desktop.
Save pocco81/98533b157790eaa6219864db1694d6db 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