How can you display host IP address in bash prompt?

I use the following in my .bashrc

THEIP=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | tail -1 | cut -d: -f2 | awk '{ print $1}')
PS1="\[\033[01;31m\]\u@"$THEIP" \w $\[\033[00m\] ";

This will show you a prompt of:

[email protected] /opt/amazon/jungle $

Remove the \w to get rid of the present working directory or make it \W to make it only the a partial working directory

[email protected] jungle $

You can also use the following, assuming that there is only one IP address in your /etc/hosts file:

THEIP=$(hostname -i)

Disclaimer: literally no attempt made to make this less-rubbish.

Below is the way to do that...

PS1=$(ifconfig $(route -n | grep ^0.0.0.0 | awk '{print $NF}') | grep inet | grep -v inet6 | awk '{print $2}')

As stated by Sirex you can do a lot of tricky things with command substitution, I would prefer the following declaration using the ip utility:

export PS1="IP: $(ip addr show dev eth0 | grep "inet " | cut -d" " -f6) #" or something like that.

Another option is to use the tool facter which provides a lot of information about your system so a simple facter ipaddress_eth0 gives you the IP-Adress. So the new example would be

export PS1="IP: $(facter ipaddress_eth0) #

facter allows you to use much more system informations for scripting if you want. Just execute facter to see what it got in it's whole configuration. If you want you can also declare your own facts in /etc/facts.d.