Answers: <p>Well... you would need to know what IP address you actually want. Is it a public IP address? Is it a private subnet address? Do you only have 1 IP address? A few things you would need to figure out.</p>
  
  <p>But, if we just assume that we want to have whatever ip address your VM thinks it's address is then:</p>
  
  <p>Add the following to your .bashrc</p>
  
  <pre><code>PS1="\[$(tput setaf 33)\][\u@$(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1) \w ]\[$(tput sgr0)\] \[$(tput setaf 34)\]\\$\[$(tput sgr0)\] "
  </code></pre>
  
  <p>To throw an IP address into your prompt.</p>
  
  <p>Now, because that's not really human friendly, let's break it down:</p>
  
  <p>Make the prompt blue (honestly I can't remember why I always wrap with <code>\[ \]</code> I just remember someone telling me I needed to do that, so I guess see what works for you:</p>
  
  <pre><code>\[$(tput setaf 33)\]
  </code></pre>
  
  <p>This bit you already had figured out:</p>
  
  <pre><code>[\u@ 
  </code></pre>
  
  <p>This insanity to find the default IP address, it's a bit hard to break down without just reading through the <code>ip</code> man pages but basically we are searching for the interface marked as default then grabbing the IP address and stripping all the extra stuff out with awk/grep/head:</p>
  
  <pre><code>$(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1)
  </code></pre>
  
  <p>I tend to want to know my ${PWD}:</p>
  
  <pre><code>\w ]
  </code></pre>
  
  <p>Reset our colour scheme just to be stafe (I've found weird stuff happens when you keep layering colours without resting:</p>
  
  <pre><code>\[$(tput sgr0)\] 
  </code></pre>
  
  <p>Set some other colour:</p>
  
  <pre><code>\[$(tput setaf 34)\]
  </code></pre>
  
  <p>Set the <code>$</code> or <code>#</code> prompt which is handy if you want it to be obvious if you are logged in as root:</p>
  
  <pre><code>\\$
  </code></pre>
  
  <p>Then reset colour again:</p>
  
  <pre><code>\[$(tput sgr0)\]
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-29T06:52:13.393Z
Answers: 2020-02-29T06:52:13.393Z
Answers: hvindin
Body: <p>I want to customise my terminal on an Amazon linux device. 
  I have colours using <code>$(tput setaf 33);</code>, user name with <code>/u</code> but the IP address not there.</p>
  
  <pre><code>#blue=$(tput setaf 33);
  PS1="\[${blue}]\u$(tput sgr0)"
  PS1="${blue}[\u@\w $(tput sgr0)"
  PS1+=""
  export PS1;
  </code></pre>
  
Tags: <bash>
Title: How do I add my IP address to the start of my terminal using the bashrc file?
UserDisplayName: doug
ThreadText: # How do I add my IP address to the start of my terminal using the bashrc file?
  
  (Asked by: username_0 on 2020-02-25)
  
  I want to customise my terminal on an Amazon linux device.
  I have colours using `$(tput setaf 33);`, user name with `/u` but the IP address not there.
  
  ```
  #blue=$(tput setaf 33);
  PS1="\[${blue}]\u$(tput sgr0)"
  PS1="${blue}[\u@\w $(tput sgr0)"
  PS1+=""
  export PS1;
  
  ```
  
  **Tags:** bash
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-02-29. Score: 2 | Accepted answer
  
  Well... you would need to know what IP address you actually want. Is it a public IP address? Is it a private subnet address? Do you only have 1 IP address? A few things you would need to figure out.
  
  But, if we just assume that we want to have whatever ip address your VM thinks it's address is then:
  
  Add the following to your .bashrc
  
  ```
  PS1="\[$(tput setaf 33)\][\u@$(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1) \w ]\[$(tput sgr0)\] \[$(tput setaf 34)\]\\$\[$(tput sgr0)\] "
  
  ```
  
  To throw an IP address into your prompt.
  
  Now, because that's not really human friendly, let's break it down:
  
  Make the prompt blue (honestly I can't remember why I always wrap with `\[ \]` I just remember someone telling me I needed to do that, so I guess see what works for you:
  
  ```
  \[$(tput setaf 33)\]
  
  ```
  
  This bit you already had figured out:
  
  ```
  [\u@ 
  
  ```
  
  This insanity to find the default IP address, it's a bit hard to break down without just reading through the `ip` man pages but basically we are searching for the interface marked as default then grabbing the IP address and stripping all the extra stuff out with awk/grep/head:
  
  ```
  $(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1)
  
  ```
  
  I tend to want to know my ${PWD}:
  
  ```
  \w ]
  
  ```
  
  Reset our colour scheme just to be stafe (I've found weird stuff happens when you keep layering colours without resting:
  
  ```
  \[$(tput sgr0)\] 
  
  ```
  
  Set some other colour:
  
  ```
  \[$(tput setaf 34)\]
  
  ```
  
  Set the `$` or `#` prompt which is handy if you want it to be obvious if you are logged in as root:
  
  ```
  \\$
  
  ```
  
  Then reset colour again:
  
  ```
  \[$(tput sgr0)\]
  
  ```
  
  
  ---
  
  