[Korn Shell] PS1 tutorial - Programming On Unix

Users browsing this thread: 1 Guest(s)
In this tutorial I will show you how to create your own PS1 in Korn Shell.

  1. The first thing you need to do is open up your .kshrc
    Code:
    sudo nano .kshrc

    Here is my .kshrc opened.

    [Image: 6Q4ss.png]

    • You do not need to use nano, you can use whatever text editor you want. I, in this tutorial will be using vim.
    • If you do not have a .kshrc, simply open your terminal and type(Make sure its in your home directory):
      Code:
      touch .kshrc


  2. Once you opened your .kshrc, you want to add this line:
    Code:
    PS1=
    Now, were going to do some fun stuff!

  3. Here are some options you can add to your PS1.
    [Image: wG1uh.png]

    To add these options to your PS1 you simply do this:
    Code:
    PS1=`option_here`

    Example:
    Code:
    PS1=`uname -n`
    • Note that to add these options its the ` `, not ' '.
    • These are not ALL the options. You can do this with any command.
      Examples:
      Code:
      PS1=`date '+%d-%h-%Y'`
      Code:
      PS1=`pwd`

  4. Now we will get into colors, and more!
    [Image: ISMTj.png]
    [Image: c27mE.png]
    [Image: SwOCZ.png]
    Note: Sorry about this guys, I am horrible in paint.

    Now you're probably asking yourself. "Wow! These are awesome features! So how to I use this black magic?"

    Well the answer is simple.

    Code:
    $'\E[Xm'
    You just replace 'X' with the color or thing you want. For example.

    Code:
    $'\E[0;31m'
    This will print out red text.

    So if you want a certain command or text to be red in your PS1, you just do this.
    Code:
    PS1=$'\E[0;31m'`logname`$'\E[0;37m'

    What this is saying is that you want red text, so your logname will be red. However everything AFTER logname will be lightgray. If you did not add that last color, everything you type will be red.

    Now, if you want to add numbers 0-5 and 22-25 to your PS1, you do the same thing.
    Code:
    PS1=$'\E[5m'`logname`$'\E[25m'
    What this says is, make logname blink, and everything AFTER logname not blink.
    Remember, if you don't add the turn off blink, everything you type will blink.
    • You can combine these codes together, for example.
      Code:
      PS1=$'\E[4;31m'`logname`$'\E[0;37m'
      Notice how I put 4;31? That is saying make it blink, with red text.
      Same in 0;37. That says make it normal text with light gray color.

    • You can do this with the background colors too.
      Code:
      PS1=$'\E[41;34m'`logname`$'\E[0;37m'
      In 41;34, this says, make it a red background, with blue text.


  5. Now what if you don't want to add any commands to your PS1? What if you just want your PS1
    to say "I love ksh >" in blue text? Well this is simple.

    All you have to do is add " " or ' ' to your PS1.

    Code:
    $'\E[0;34m'"I love ksh > "$'\E[0;37m'

    Or:
    Code:
    PS1=$'\E[0;34m''I love ksh > '$'\E[0;37m'

    And that's it! Now you can start creating your OWN PS1.
    Just in case you're curious. Here is my old PS1.

    Code:
    PS1='['$'\E[33m'`logname`$'\E[37m'''@''$'\E[34m'`hostname -s`$'\E[37m']'--» '

    What this is saying is print '[' then make everything after that Yellow.
    Then print my logname in yellow text. Then it says, make everything
    after logname light gray. It will now print @ in light gray. After that it
    says everything after @ to be blue. Now it will print my hostname in
    blue. And lastly, everything after my hostname to be in light gray,
    it will then print --» and everything I type in gray.

    Result:
    [Image: BugVi.png]

    I hope this tutorial has helped anyone out there. I tried to make it as easy as I can so everyone can understand.

    If you created your .kshrc, and nothing seems to be working. Create or open your .profile.
    If you don't have a .profile. Do this.
    Code:
    touch .profile

    And add this:
    Code:
    ENV=$HOME/.kshrc;  export ENV
    Make sure its in your home directory.