Score:0

How to Make the IP Address Appear as Part of Bash Prompt?

ke flag

Running an Amazon EC2 instance with Ubuntu 22.04. The elastic ip address is 52.120.94.72.

I want the bash prompt to read like this (no space)

[email protected]:~/working_directory$

Based on some advice on the net, I edited the top of the /etc/hosts file like this

127.0.0.1 localhost 52.120.94.72

and even added the following line to .bashrc

export PS1='\[\033[0;32m\]\u@\h:\[\033[36m\]\W\[\033[0m\] \$ '

But it is not working, and currently, the bash prompt looks like

della@52:~ $

which is ridiculous. How to make it appear like I want. Also, is the $HOSTNAME environment variable important in this context? Particularly, if I run some web server in this instance, I can make calls just using the ip address, right? No need for the hostname.

Score:3
la flag

Letting your prompt actually derive that IP-address is, as far as I know, rather more difficult. Bash does not provide a ready-made variable to use for that according to the list with special characters here.

I think the easiest is to simply hardcode the IP-address you want to see in your prompt definition:

 export PS1='\[email protected]:\w\$'

where \u, \$ and \w are some of those special characters that get expanded by your bash shell according to the definitions from here :

  • \u
    The username of the current user.
  • \w The value of the PWD shell variable ($PWD), with $HOME abbreviated with a tilde ~ (uses the $PROMPT_DIRTRIM variable).
  • \$
    If the effective uid is 0, #, otherwise $.

With the additional escapes in the example you quoted:

 export PS1='\[\033[0;32m\]\[email protected]:\[\033[36m\]\w\[\033[0m\]\$'

you get a prompt formatted with ANSI escape sequences/codes that instruct the terminal program you're using to apply specific formatting and colouring.

See https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences for a detailed overview.


You could of course rely on external helper to derive your IP, such as query the instance meta data URI :

MY_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
export PS1='\u@$MY_IP:\w\$' 
Julian avatar
ht flag
This definitely put me in the right direction - a bit more dynamically, you could use `export ELASTIC_IP=$(curl ipinfo.io/ip)`, then to set it you use `PS1='\u@$ELASTIC_IP:\w\$'`. If you put this in your .bashrc or profile with the export, you also have access to that value throughout your session - `echo $ELASTIC_IP`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.