1

I've searched for quite a while, but haven't been able to find a post or any information whatsoever about adding a variable that is, well, variable (i.e. changing) within PS1 in bash, that will update every time a new prompt occurs. In concreto, I would like the width of the prompt to span across the whole terminal window, e.g.:

7zS2::awesome| --------------------------------------------------------- ~/.config/awesome

This is what I have so far, omitting color codes for legibility:

mytest=$PWD
mynext="$(basename $PWD)"
mylength=$((${#mytest}+${#mynext}))
length=$(($mylength+6))
PS1='7zS2::\W| $(printf "\\u2500%.0s" $(seq $length $(tput cols))) \w\n\$'

Which works perfectly whenever I

exec bash

to reset the prompt.

However, I would like it to work without me having to reload bash. Surely there must be a way of doing this, as \w, \W and of the likes are unique and updated every prompt as well. Any way of easily doing this? Thanks!

7zS2

7z.
  • 85
  • 1
  • 6

1 Answers1

1

Try this. Place your code into a file, I'm calling it gash.sh in my home directory, with a small modification: replace the PS1 assignment with echo, and replace the single quotes with double.

mytest=$PWD
mynext="$(basename $PWD)"
mylength=$((${#mytest}+${#mynext}))
length=$(($mylength+6))
echo "7zS2::\W| $(printf \\u2500%.0s $(seq $length $(tput cols))) \w\n\$"

then in your startup file set PS1:

PS1='$(~/gash.sh)'
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Nice and quick fix, thanks! Only problem now is that it's printing it out weirdly, but the length is perfect every time. This is what it currently looks like: ?------------------------------------\ \w\n$33[m\]| ----------------------------- – 7z. Feb 11 '16 at 11:11
  • I.e. it won't read in the escape codes nor color options. Any suggestions on this one? Thank you – 7z. Feb 11 '16 at 11:13