7

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?

2
  • 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

11

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)
2
  • 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 '127.0.0.1' | tail -1 | awk '{ print $2}')
    – geekbrit
    Commented Jun 23, 2018 at 13:58
5

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}')
0
4

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.

1

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
1

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

IP=$(hostname -i)
PS1='$IP'
0

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.

0

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\$ "
1
0

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

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

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

IF=$1
STATUS=$2

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
fi

Then in your .bashrc:

RED='\033[0;31m'
NC='\033[0m' # No Color
WHITE='\e[97m'
GREEN='\e[0;32m'

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

0

this works for me:

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

if [[ $firstread || $elapsedSeconds -gt 1800 ]]
then
        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
fi

PS1='\[\033[01;32m\]\u@`cat /tmp/ip/last`\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '````
2
  • 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
-2

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
    (1) Why would exporting help?  (2) You know that 192.168.1.127 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 .