bashpromptps1

Get actual prompt length


I wanted to print something like this on my gnome-terminal

[abc@host pwd]$ ************************************************************

using some manipulations in PS1 but the number of stars are dynamic depending on PS1 and terminal width. So can anyone plz suggest me the way to find out the length of PS1 i.e the actual string which will be displayed.


Solution

  • It can't ve done portably IMO, as PS1 can contain escape sequences for color codes, (multiple) new lines too.

    It can be calculated. But that's a really hard task. What if the user codes colorcodes in variable names (it's a common scenario), how to decide (during evaluation/counting the length) if that's something that the user wants to display or is only style information?