Like above, I'd like to display IPv4 address of eth0 (or part of it) in PS1 or bash. Is there a way to do this?

  • 1
    define: ip address -- of which interface ? ipv4 / ipv6 ? localhost too ? – Sirex Nov 1 '13 at 3:55
  • Is that supposed to say "in PS1 on (or in) bash"? Just checking. – beroe Sep 5 '14 at 4:40

I use the following in my .bashrc

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

This will show you a prompt of:

root@ /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

root@ jungle $

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

THEIP=$(hostname -i)
  • Interesting solutions. Not sure what OS the OP is using, but in OSX, hostname doesn't have the -i option. You could do something silly like host $(hostname) | cut -f 4 -d " " if there is a DNS available. – beroe Sep 5 '14 at 4:44
  • The ifconfig output format has changed slightly (I'm using bash in ubuntu 18.04), and the 'cut' command does not seem necessary if the awk command is adjusted slightly, so I am using: THEIP=$(ifconfig | grep 'inet '| grep -v '' | tail -1 | awk '{ print $2}') – geekbrit Jun 23 '18 at 13:58

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

Below is the way to do that...

PS1=$(ifconfig $(route -n | grep ^ | 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.


I've taken @SomeGuyOnAComputer's answer and improved it slightly:

IP=$( ifconfig | grep ^eth -A2 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }' ) PS1='\n\[\033[00;32m\]\u@$IP \[\033[00;33m\]\w\n\\$\[\033[00m\] '

I had a docker container running on my machine so it was picking up the docker's ip address when I ran @SomeGuyOnAComputer's command. The new version looks for the line that starts with 'eth' and prints the 2 lines after the matching line (-A2) information. The rest is just like @SomeGuyOnAComputer's version.


I am using this config in .bashrc and is pretty cool (similar to suggested above)

pyclean () {

        find . -type f -name "*.py[co]" -delete -print
        find . -type f -name ".DS_Store" -delete -print

        find . -type d -name "__pycache__" -delete -print

IP=$( ifconfig | grep ^eth -A2 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }' )
export PS1="\e[1;32m\D{%T} @$IP\e[1;34m\w/\e[m\n\$ "

You may also like this-

IP=$( ifconfig | grep ^eth -A2 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }' )
PS1='\e[1;34m\D{%T} \[\033[00;39m\]\u\[\033[00;32m\]@$IP \[\033[00;33m\]\w/\n\\$\[\033[00m\] '

IP Address in Prompt With Automatic Updating

My solution was as follows (works on Fedora). Adapted from the the other answers here and info from this post http://sysadminsjourney.com/content/2008/12/18/use-networkmanager-launch-scripts-based-network-location/.

Create the following script as root: /etc/NetworkManager/dispatcher.d/99smartsynergy.sh

# http://sysadminsjourney.com/content/2008/12/18/use-networkmanager-launch-scripts-based-network-location/


if [ "$STATUS" = "up" ]; then
        ip=$(/usr/sbin/ip addr show dev $IF | grep "inet " | cut -d" " -f6)

        echo -n $ip > /tmp/ip_address.txt
        chmod a+w /tmp/ip_address.txt

Then in your .bashrc:

NC='\033[0m' # No Color

touch /tmp/ip_address.txt
export PS1='\['$GREEN'\][\d \t \u@\h \W][\'$WHITE'\]$(cat /tmp/ip_address.txt)[\'$NC'\] \$ \[\e[m\]'


Export your IP variable: export MYIP=$(ifconfig | grep inet | egrep -v "127|inet6" | awk '{print $2}') Export your bash variable: export PS1='[\u@"$MYIP" \W]\$ '

  • (1) Why would exporting help?  (2) You know that is a valid IP address, right? – G-Man Oct 24 '17 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.