Colored Prompt in KornShell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
11
down vote

favorite
7












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 "33[34mLinux33[00m"


gives a blue "Linux" output, as does:



printf "33[34mLinux33[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.










share|improve this question



























    up vote
    11
    down vote

    favorite
    7












    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 "33[34mLinux33[00m"


    gives a blue "Linux" output, as does:



    printf "33[34mLinux33[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.










    share|improve this question

























      up vote
      11
      down vote

      favorite
      7









      up vote
      11
      down vote

      favorite
      7






      7





      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 "33[34mLinux33[00m"


      gives a blue "Linux" output, as does:



      printf "33[34mLinux33[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.










      share|improve this question















      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 "33[34mLinux33[00m"


      gives a blue "Linux" output, as does:



      printf "33[34mLinux33[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.







      colors prompt ksh






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 11:33









      Community

      1




      1










      asked Jul 5 '12 at 18:19









      Kazark

      5902829




      5902829




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          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 "33[35mLinux33[00m")"





          share|improve this answer




















          • 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

















          up vote
          4
          down vote













          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 33.



          PS1=$(print '33[34mLinux33[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[34mLinuxe[00m'





          share|improve this answer


















          • 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

















          up vote
          2
          down vote













          I use these in mkshfor a user shell:



          # custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
          PS1=$'arae[1;34ma ^ ^ ^ ^ | ae[36ma$@$HOSTNAME%%.*ae[34ma | ^ ^ ^ ^ | ae[0;33ma$(local d=$PWD:-? p=~; [[ $p = ?(*/) ]] || d=$d/#$p/~;
          print -nr -- "$d")ae[1;34ma |n ^ ^ ^ ^ | ae[32ma$(date +%H:%M)ae[34ma | ^ ^ >>ae[0ma '


          & a slightly different shell for root:



          PS1=$'arae[1;34ma ^ ^ ^ ^ ^ ae[31ma$ echo ?)@$HOSTNAME%%.*ae[34ma ^ ^ ^ ^ ^ ^ ^ ae[0;33ma$(
          local d=$PWD:-? p=~
          [[ $p = ?(*/) ]] || d=$d/#$p/~
          print -nr -- "$d"
          )ae[1;34ma ^ ^ n ^ ^ ^ ^ ^ ae[32ma$(date +%H:%M)ae[34ma ^ ^ ^ ^ ae[0ma '


          enter image description here



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






          share|improve this answer





























            up vote
            1
            down vote













            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.







            share|improve this answer



























              up vote
              0
              down vote













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



              Korn shell prompt:



              PS1="$(echo "33[32m`logname`33[35m@33[32m`hostname` 33[33m`pwd`33[36mn$33[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$_$$"





              share|improve this answer








              New contributor




              skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















                Your Answer








                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "106"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f42399%2fcolored-prompt-in-kornshell%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                10
                down vote



                accepted










                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 "33[35mLinux33[00m")"





                share|improve this answer




















                • 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














                up vote
                10
                down vote



                accepted










                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 "33[35mLinux33[00m")"





                share|improve this answer




















                • 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












                up vote
                10
                down vote



                accepted







                up vote
                10
                down vote



                accepted






                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 "33[35mLinux33[00m")"





                share|improve this answer












                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 "33[35mLinux33[00m")"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 5 '12 at 18:28









                manatwork

                21.4k38284




                21.4k38284











                • 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
















                • 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















                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




                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












                up vote
                4
                down vote













                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 33.



                PS1=$(print '33[34mLinux33[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[34mLinuxe[00m'





                share|improve this answer


















                • 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














                up vote
                4
                down vote













                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 33.



                PS1=$(print '33[34mLinux33[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[34mLinuxe[00m'





                share|improve this answer


















                • 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












                up vote
                4
                down vote










                up vote
                4
                down vote









                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 33.



                PS1=$(print '33[34mLinux33[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[34mLinuxe[00m'





                share|improve this answer














                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 33.



                PS1=$(print '33[34mLinux33[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[34mLinuxe[00m'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 27 '14 at 14:08

























                answered Jul 6 '12 at 1:21









                Gilles

                520k12510381569




                520k12510381569







                • 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




                  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




                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




                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




                @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










                up vote
                2
                down vote













                I use these in mkshfor a user shell:



                # custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
                PS1=$'arae[1;34ma ^ ^ ^ ^ | ae[36ma$@$HOSTNAME%%.*ae[34ma | ^ ^ ^ ^ | ae[0;33ma$(local d=$PWD:-? p=~; [[ $p = ?(*/) ]] || d=$d/#$p/~;
                print -nr -- "$d")ae[1;34ma |n ^ ^ ^ ^ | ae[32ma$(date +%H:%M)ae[34ma | ^ ^ >>ae[0ma '


                & a slightly different shell for root:



                PS1=$'arae[1;34ma ^ ^ ^ ^ ^ ae[31ma$ echo ?)@$HOSTNAME%%.*ae[34ma ^ ^ ^ ^ ^ ^ ^ ae[0;33ma$(
                local d=$PWD:-? p=~
                [[ $p = ?(*/) ]] || d=$d/#$p/~
                print -nr -- "$d"
                )ae[1;34ma ^ ^ n ^ ^ ^ ^ ^ ae[32ma$(date +%H:%M)ae[34ma ^ ^ ^ ^ ae[0ma '


                enter image description here



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






                share|improve this answer


























                  up vote
                  2
                  down vote













                  I use these in mkshfor a user shell:



                  # custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
                  PS1=$'arae[1;34ma ^ ^ ^ ^ | ae[36ma$@$HOSTNAME%%.*ae[34ma | ^ ^ ^ ^ | ae[0;33ma$(local d=$PWD:-? p=~; [[ $p = ?(*/) ]] || d=$d/#$p/~;
                  print -nr -- "$d")ae[1;34ma |n ^ ^ ^ ^ | ae[32ma$(date +%H:%M)ae[34ma | ^ ^ >>ae[0ma '


                  & a slightly different shell for root:



                  PS1=$'arae[1;34ma ^ ^ ^ ^ ^ ae[31ma$ echo ?)@$HOSTNAME%%.*ae[34ma ^ ^ ^ ^ ^ ^ ^ ae[0;33ma$(
                  local d=$PWD:-? p=~
                  [[ $p = ?(*/) ]] || d=$d/#$p/~
                  print -nr -- "$d"
                  )ae[1;34ma ^ ^ n ^ ^ ^ ^ ^ ae[32ma$(date +%H:%M)ae[34ma ^ ^ ^ ^ ae[0ma '


                  enter image description here



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






                  share|improve this answer
























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I use these in mkshfor a user shell:



                    # custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
                    PS1=$'arae[1;34ma ^ ^ ^ ^ | ae[36ma$@$HOSTNAME%%.*ae[34ma | ^ ^ ^ ^ | ae[0;33ma$(local d=$PWD:-? p=~; [[ $p = ?(*/) ]] || d=$d/#$p/~;
                    print -nr -- "$d")ae[1;34ma |n ^ ^ ^ ^ | ae[32ma$(date +%H:%M)ae[34ma | ^ ^ >>ae[0ma '


                    & a slightly different shell for root:



                    PS1=$'arae[1;34ma ^ ^ ^ ^ ^ ae[31ma$ echo ?)@$HOSTNAME%%.*ae[34ma ^ ^ ^ ^ ^ ^ ^ ae[0;33ma$(
                    local d=$PWD:-? p=~
                    [[ $p = ?(*/) ]] || d=$d/#$p/~
                    print -nr -- "$d"
                    )ae[1;34ma ^ ^ n ^ ^ ^ ^ ^ ae[32ma$(date +%H:%M)ae[34ma ^ ^ ^ ^ ae[0ma '


                    enter image description here



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






                    share|improve this answer














                    I use these in mkshfor a user shell:



                    # custom prompt see http://comments.gmane.org/gmane.os.miros.mksh/126
                    PS1=$'arae[1;34ma ^ ^ ^ ^ | ae[36ma$@$HOSTNAME%%.*ae[34ma | ^ ^ ^ ^ | ae[0;33ma$(local d=$PWD:-? p=~; [[ $p = ?(*/) ]] || d=$d/#$p/~;
                    print -nr -- "$d")ae[1;34ma |n ^ ^ ^ ^ | ae[32ma$(date +%H:%M)ae[34ma | ^ ^ >>ae[0ma '


                    & a slightly different shell for root:



                    PS1=$'arae[1;34ma ^ ^ ^ ^ ^ ae[31ma$ echo ?)@$HOSTNAME%%.*ae[34ma ^ ^ ^ ^ ^ ^ ^ ae[0;33ma$(
                    local d=$PWD:-? p=~
                    [[ $p = ?(*/) ]] || d=$d/#$p/~
                    print -nr -- "$d"
                    )ae[1;34ma ^ ^ n ^ ^ ^ ^ ^ ae[32ma$(date +%H:%M)ae[34ma ^ ^ ^ ^ ae[0ma '


                    enter image description here



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







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 5 '16 at 11:02









                    z3ntu

                    5315




                    5315










                    answered Aug 3 '15 at 16:42









                    Stuart Cardall

                    72659




                    72659




















                        up vote
                        1
                        down vote













                        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.







                        share|improve this answer
























                          up vote
                          1
                          down vote













                          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.







                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            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.







                            share|improve this answer












                            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.








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 28 '16 at 3:24









                            Derek Peschel

                            111




                            111




















                                up vote
                                0
                                down vote













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



                                Korn shell prompt:



                                PS1="$(echo "33[32m`logname`33[35m@33[32m`hostname` 33[33m`pwd`33[36mn$33[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$_$$"





                                share|improve this answer








                                New contributor




                                skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.





















                                  up vote
                                  0
                                  down vote













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



                                  Korn shell prompt:



                                  PS1="$(echo "33[32m`logname`33[35m@33[32m`hostname` 33[33m`pwd`33[36mn$33[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$_$$"





                                  share|improve this answer








                                  New contributor




                                  skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.



















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









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



                                    Korn shell prompt:



                                    PS1="$(echo "33[32m`logname`33[35m@33[32m`hostname` 33[33m`pwd`33[36mn$33[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$_$$"





                                    share|improve this answer








                                    New contributor




                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









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



                                    Korn shell prompt:



                                    PS1="$(echo "33[32m`logname`33[35m@33[32m`hostname` 33[33m`pwd`33[36mn$33[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$_$$"






                                    share|improve this answer








                                    New contributor




                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    share|improve this answer



                                    share|improve this answer






                                    New contributor




                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered yesterday









                                    skeetastax

                                    1




                                    1




                                    New contributor




                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    skeetastax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f42399%2fcolored-prompt-in-kornshell%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown






                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay