11

According to this StackOverflow post, it is possible have a colored prompt in KornShell. I have not been able to figure out how to do this. I am able to use color:

echo -e "\033[34mLinux\033[00m"

gives a blue "Linux" output, as does:

printf "\033[34mLinux\033[00m"

However, when I incorporate the escape codes into my PS1 prompt variable, they are not being escaped. What do I need to do to get a colored prompt? Besides being something of a sucker for eyecandy, I find that a colored prompt is useful when visually parsing output.

10

Just use a literal Esc character, entered with Ctrl-v,Esc (will be displayed as ^[ on the screen):

PS1="^[[34mLinux^[[00m"

Or use the output of the echo command you find out is working:

PS1="$(echo -e "\033[35mLinux\033[00m")"
  • It's at times like this that I realize I am yet a commandline noob. I usually feel so good in comparison to my friends. :) The first version worked nicely; what a neat trick! I didn't have success with the second version. +1 – Kazark Jul 5 '12 at 18:57
5

You need to put a literal escape character in the PS1 variable. Ksh88 and clones such as pdksh and mksh (older versions) have no literal syntax for control characters except through the print built-in. Mksh understands \e for escape, but pdksh requires the octal code \033.

PS1=$(print '\033[34mLinux\033[00m')

ATT ksh93 introduces the backlash-escaped literal syntax $'…' (also available in mksh since R39b). You can use backslash escapes to put control characters in these literals.

PS1=$'\e[34mLinux\e[00m'
  • 2
    Actually, mksh also supports $'…' – admittedly because Dave Korn (the Korn in Korn Shell) insisted I add it. – mirabilos Feb 27 '14 at 14:01
  • @mirabilos Ah, thanks. I probably typed that answer on a machine with mksh R39 and didn't check newer versions. – Gilles Feb 27 '14 at 14:09
2

I use these in mkshfor a user shell:

# custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
PS1=$'\a\r\a\e[1;34m\a ^ ^  ^ ^ | \a\e[36m\a${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo
\?)}@${HOSTNAME%%.*}\a\e[34m\a | ^ ^  ^ ^ | \a\e[0;33m\a$(local d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || d=${d/#$p/~};
print -nr -- "$d")\a\e[1;34m\a |\n ^ ^  ^ ^ | \a\e[32m\a$(date +%H:%M)\a\e[34m\a | ^ ^ >>\a\e[0m\a '

& a slightly different shell for root:

PS1=$'\a\r\a\e[1;34m\a ^ ^  ^ ^  ^   \a\e[31m\a${USER:=$(ulimit -c 0; \
    id -un 2>/dev/null || echo \?)}@${HOSTNAME%%.*}\a\e[34m\a  ^ ^  ^ ^  ^ ^  ^   \a\e[0;33m\a$(
        local d=${PWD:-?} p=~
        [[ $p = ?(*/) ]] || d=${d/#$p/~}
        print -nr -- "$d"
)\a\e[1;34m\a  ^ ^ \n ^ ^  ^ ^  ^   \a\e[32m\a$(date +%H:%M)\a\e[34m\a  ^ ^  ^ ^   \a\e[0m\a '

enter image description here

As the special characters did not copy, here is a pastebin paste for both the normal user and root.

1

Several things are going on in people's answers, each involving a different syntax.

  1. The escape sequences for your terminal program given in the documentation.

  2. How to encode the terminal escape sequences in the PS1 variable, for example \e or literal escape. This depends on the shell.

  3. How to add changing information to the prompt, for example the hostname, current directory, etc. This also depends on the shell. You may be able to use special backslash sequences like \w for the working directory, or you may have to name environment variables or embed command lines into the string.

  4. How to tell the shell which characters print and which characters don't. The shell doesn't know where your terminal escape sequences begin and end. If the shell has a command-line editor or something similar, it needs to know which characters print so it knows where the cursor is. Without this detail, your prompt might look right but editing can behave strangely.

    • In bash, you put \[ before each escape sequence and \] after.

    • In ksh, you choose a single nonprinting ASCII character that can't occur anywhere else in the string. At the beginning of the string, you write that character followed by an ASCII carriage return. Then you write the same character before and after each escape sequence. z3ntu chose \a as the marker character.

0

Thanks to previous answers and other sources, I was able to come up with these:

Korn shell prompt:

PS1="$(echo "\033[32m`logname`\033[35m@\033[32m`hostname` \033[33m`pwd`\033[36m\n\$\033[0m")"

Bash prompt:

PS1="\[\e[32m\]\u\[\e[35m\]@\[\e[32m\]\h \[\e[33m\]\w\[\e[36m\]\n\$\[\e[0m\]"

These give the same coloured prompt (yes, I speak Queen's English ;) ) in the format:

username@computername /current/working/directory
$

The DOS equivalent (though there are no colour options) is:

prompt %username%@%computername% $P$_$G

which gives:

username@computername /current/working/directory
>

The ">" is keeping it 'DOSsy' rather than 'nixy' with "$", but if you wanted the sigil ("$") instead then it would be:

prompt %username%@%computername% $P$_$$

Provided your login account has the requisite permissions, to make the 'nixies' permanent prepend them with "export ":

export PS1="..."

and change the DOSsy to:

setx PROMPT "%username%@%computername% $P$_$G"

or

setx PROMPT "%username%@%computername% $P$_$$"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.