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
    Commented Nov 1, 2013 at 3:55
  • Is that supposed to say "in PS1 on (or in) bash"? Just checking.
    – beroe
    Commented Sep 5, 2014 at 4:40

11 Answers 11


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:

[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)
  • 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
    Commented Sep 5, 2014 at 4:44
  • 2
    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
    Commented Jun 23, 2018 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.


In my case: I have some VMs run Ubuntu server, each of them have private it's IPv4 address, and it should be nice if I can see the IP address each time I login (like EC2 Instances), and here is what I did:

  • Add the following lines in the bottom of ~/.bashrc file:
IP=$(hostname -I | awk '{print $1}')
PS1="\u@$IP:\w\$ "
  • Then type the command: source ~/.bashrc, and the IP is displayed
  • I also use rsync to sync the .bashrc between multiple machines (examples: rsync -avzhe ssh /path/to/file user@server:/path/to/destination), hope it helps

This is the minimalist way. Feel free to add whatever other characters you need in PS1.

IP=$(hostname -i)

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\]'


this works for me:

if [ ! -f "/tmp/ip/last" ]
        lastModificationSeconds=$(date -r /tmp/ip/last +%s)
        currentSeconds=$(date +%s)
        ((elapsedSeconds = currentSeconds - lastModificationSeconds))

if [[ $firstread || $elapsedSeconds -gt 1800 ]]
        mkdir -p /tmp/ip >/dev/null 2>&1
        curl -s  ifconfig.me > /tmp/ip/last
        chmod a+rw /tmp/ip /tmp/ip/last >/dev/null 2>&1

PS1='\[\033[01;32m\]\u@`cat /tmp/ip/last`\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '````
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 16, 2022 at 20:35
  • not sure what is unclear, Mr. bot. please be more specific
    – nir
    Commented Dec 18, 2022 at 21:26

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
    (1) Why would exporting help?  (2) You know that is a valid IP address, right? Commented Oct 24, 2017 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .