Bash, always display a color bar at the top of the screen

In any case, to modify the bash profile scripts so that the color bar at the top of the screen is always displayed. I have a requirement to show the colored hostname, username and ipaddress on the screen at any time, but I do not want to overload PS1, as this will make the request more than half the default console width.

+4
source share
4 answers

Not perfect, but it shows you how to fix part of your prompt on the first line of the screen:

PS1='\w \[\e[s\e[1;1H\e[42m\]\h \u ipaddress\[\e[0m\e[u\]\$ ' 

Breakdown:

  • \e[s - keep current cursor position
  • \e[1;1H - move the cursor to row 1, column 1 (numbered from the upper left corner
  • \e[u - restore the cursor to a previously saved position
  • \e42m - make a green background
  • \e0m - restore foreground / background colors to default
  • \[...\] - enclose various non-printable characters so that bash can correctly calculate the length of the prompt.

Wikipedia lists other escape codes. Two things missing from this answer are how to extend the panel to the end and how to set the IP address correctly.

Update: I believe this covers the changes made by ruckc:

 PS1='\[\e[s\e[1;1H\e[42m\e[K\h \u ipaddress\e[0m\e[u\]\w \$ ' 
+4
source

How to add \ n inside your PS1 so you always use a new line with full width?

+3
source

if you are looking for something less hacked (but possibly overkill), consider byobu

https://en.wikipedia.org/wiki/Byobu_(software)

0
source

Alternatively, if you use xterms, you can install xterm instead:

 export PS1="\[\033]0;\u $(host $(hostname))\007\]\ u@ \h:\w\$ " 

This sets the xterm header and prompts for the username @host: pwd.

My.bashrc contains something like this, so PS1 installs correctly depending on whether we are in xterm or not:

 if [[ -n "$TERM" ]] ; then if ( echo $TERM | $GREP -q xterm ) ; then export PS1="\[\033]0;\ u@ \h:\w\007\]\ u@ \h:\w\$ " else export PS1="\ u@ \h:\w\$ " fi fi 
0
source

Source: https://habr.com/ru/post/1501935/


All Articles