0

I want to configure my prompt (PS1) in two lines. At the end of first line I want to fill all the blank space (from end of line to right hand side border of terminal) with a line (like this: _______ )

How can I do that?

currently my prompt is:

export PS1="$(echo "\033[37mroot@\033[34m`hostname`:\033[31m\${PWD}# \033[0m")"

which prints:

root@myhost:/export/home/myid# 

I want it to be:

root@myhost:/export/home/myid____________________________________________
#

later on I plan to add more things after PWD.

systems is solaris 10, shell is eksh

5.10 Generic_144488-17 sun4v sparc SUNW
#eksh --version
  version         sh (AT&T Research) 93t+ 2009-05-01

I have found something here, but it is not working. line_Across_terminal

4
  • I'm not sure I understand your problem. Can't you just add the string you want after the ${PWD}? Commented Sep 9, 2016 at 17:28
  • well how would i know the length of the string?? when terminal size changes, it should change. Commented Sep 9, 2016 at 17:37
  • wiki.bash-hackers.org/snipplets/print_horizontal_line but this is not working.... :( Commented Sep 9, 2016 at 17:39
  • I would think you could use PS2 to have the '#', so no need for a \n in PS1. As for a variable sized line to fill out your terminal, I think you might need a function that would be able to determine the size of the text and then add the line based on the current terminal size. I don't know what you would do though to adjust that when the terminal is resized. Commented Sep 9, 2016 at 19:46

1 Answer 1

1

For ksh93 (like your eksh apparently and /usr/bin/ksh on Solaris 11):

LOGNAME=$(logname)
HOSTNAME=$(hostname)

PS1='$(printf "\e[37m%s:\e[34m%s\e[4m%*s\e[m\n# " \
  "$LOGNAME@$HOSTNAME" "$PWD" \
  "COLUMNS - ${#LOGNAME} - ${#HOSTNAME} - ${#PWD} - 2")'

ksh88 (like /usr/bin/ksh on Solaris 10) doesn't have $COLUMNS, and doesn't expand command substitutions in $PS1 (though it does parameter expansion), but you could do something like:

LOGNAME=$(logname)
HOSTNAME=$(hostname)
get_COLUMNS() {
  COLUMNS=$(stty -a)
  COLUMNS=${COLUMNS#*columns = }
  COLUMNS=${COLUMNS%%;*}
}
get_COLUMNS
trap get_COLUMNS WINCH
ESC=$(printf '\33')
s=
while ((${#s}<300)); do
  pad[${#s}]=$s
  s="$s "
done
PS1="$ESC[37m\$LOGNAME@\$HOSTNAME:$ESC[34m\$PWD$ESC[4m\
"'${pad[COLUMNS - ${#LOGNAME} - ${#HOSTNAME} - ${#PWD} - 2]}'"$ESC[m
# "

(that one should also work with ksh93 and bash. First one tested successfully with /usr/bin/ksh on Solaris 11 (93u 2011-02-08) and on Debian (93u+ 2012-08-01) and should work with 93t+ 2009-05-01 as well. Second one tested successfully with /usr/bin/ksh (M-11/16/88i) and /usr/dt/bin/dtksh (93d) on Solaris 10)

6
  • produces this:... # LOGNAME=$(logname);HOSTNAME=$(hostname);PS1='$(printf "\e[37m%s:\e[34m%s\e[4m%*s\e[m\n# " "$LOGNAME@$HOSTNAME" "$PWD" "COLUMNS - ${#LOGNAME} - ${#HOSTNAME} - ${#PWD} - 2")' $(printf \e[37m%s:\e[34m%s\e[4m%*s\e[m\n# myid@host /export/home/myid COLUMNS - 6 - 10 - 19 - 2) $(printf \e[37m%s:\e[34m%s\e[4m%*s\e[m\n# myid@host /export/home/myid COLUMNS - 6 - 10 - 19 - 2) Commented Sep 9, 2016 at 22:34
  • 1
    @rajeev, that's ksh88 (like /usr/bin/ksh on Solaris 10) behaviour you're showing. ksh93 does expand command substitution in $PS1. Even the 20 year old one in /usr/dt/bin/dtksh does (though you'd need to replace \e with \33 there and it doesn't set $COLUMNS by itself). Commented Sep 9, 2016 at 23:05
  • I am having: 5.10 Generic_144488-17 sun4v sparc SUNW #eksh --version version sh (AT&T Research) 93t+ 2009-05-01 Commented Sep 9, 2016 at 23:07
  • also when i run it from a script, i get nothing. so when i run from command prompt i get these errors. Commented Sep 9, 2016 at 23:08
  • 1
    @rajeev, setting PS1 in a script has no effect, $PS1 is to set the prompt of an interactive shell. What you can do is put that code in a file and source it from your interactive shell like . ./that-file. I've added a ksh88 version. Commented Sep 9, 2016 at 23:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.