Shell The bourne shell prompt color

I've had difficulty trying to set that one. It can temporarily be set by the command line. For sh and ksh, everything may have to be logged in or maybe as far as rebooting after adjusting the configuration files. CSH, MKSH, ZSH are easy to set up the colors of the prompt. SH and KSH have been more difficult, as the settings haven't stayed for me from the configuration file and after logging into the shell. Setting up the way ls (not the prompt) is temporarily displayed in SH and KSH is easy, however, as that's universal for most shells.

set and export will show you which variables contain settings for prompts including of colors. They can also be used to adjust those variables. They show up after manually entering them. When I tried plain Bourne shell, it seemed to pick up colors that I set on the default shell that I use.

There's a lot at https://forums.freebsd.org/threads/colorize-your-bsd-shell.85458/ that you can use or adapt. Finding plain Bourne on the Internet was difficult.

https://forums.freebsd.org/threads/share-your-shrc-kshrc-or-mkshrc-file-bourne-or-korn-shell.85467/ has an example for ksh and mksh. They're similar enough and use many of the same settings that those can be tried for sh. Those and zsh can work, because ksh, mksh and zsh take after a lot of sh configurations. The ones that would be so different, they wouldn't be relevant are CSH and Bash.

In .shrc, the following an be tried:
Code:
PS1=$'\E[1m\E[32m$PWD \E[0m$ '
That's for mksh, and there's a similar one for ksh. It may need a reboot or something for it to work with sh and ksh. There may be a security setting for those shells that makes it so they're more difficult to set immediately. I'm not sure.

Update us on what works for you.
 
  • Thanks
Reactions: amr
Hello,
It works only for csh which root is using. but for bourne shell I've customized my prompt in .shrc
Code:
PS1="\u@\h[\w] % "
alias ls='ls -FG'
the output is:
Screenshot_20220722_181851.png
 
For my .cshrc:
Code:
set prompt = "%{\033[1;36m%}%~ %{\033[0m%}%# "
PS1 may be universal for prompts, perhaps outside of Bourne Shells too. Csh is so much more different than Bourne. Bourne or Bourne based (including Korn based) shells of sh, ksh, mksh and zsh use many of the same configuration settings.

Csh may pick up those settings without requiring a reboot. I've had trouble with sh and ksh.

Try typing set on the command line, and look for the variable that reads PS1. Then, try echo $PS1. See if when you adjusted the config file that it went through. You can do this for the export command (replacing set) for other variables too.
 
You can't do this with sh(1), it strips any "zero-width" characters from the prompt (which includes ESC) before printing it. A short explanation of the reasons can be found in PR 211360.

In general (with shells actually supporting this like bash, zsh, ...), instead of hacking escape sequences into your PS1, better use tput(1) (which uses termcap(5)), so you'll always have the correct sequences for your current terminal. Could look e.g. like this:

Code:
TC_NORM="$(tput me)"
TC_RED="$(tput AF 1)"
TC_GREEN="$(tput AF 2)"
TC_YELLOW="$(tput AF 3)"
TC_BLUE="$(tput AF 4)"
TC_MAGENTA="$(tput AF 5)"
TC_CYAN="$(tput AF 6)"
TC_WHITE="$(tput AF 7)"

PS1="${TC_CYAN}\u${TC_RED}@${TC_YELLOW}\h${TC_GREEN}%${TC_NORM} "
 
Back
Top