How to custom display prompt in KornShell to show hostname and current directory?

前端 未结 7 761
一个人的身影
一个人的身影 2021-02-04 09:59

I am using KornShell (ksh) on Solaris and currently my PS1 env var is:

PS1=\"${HOSTNAME}:\\${PWD} \\$ \"

And the prompt displays: hostname:/fu

7条回答
  •  生来不讨喜
    2021-02-04 10:47

    Okay, a little old and a little late, but this is what I use in Kornshell:

    PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
    

    This makes a prompt that's equivalent to PS1="\u@\h:\w\n$ " in BASH.

    For example:

    qazwart@mybook:~
    $ cd bin
    qazwart@mybook:~/bin
    $ cd /usr/local/bin
    qazwart@mybook:/usr/local/bin
    $ 
    

    I like a two line prompt because I sometimes have very long directory names, and they can take up a lot of the command line. If you want a one line prompt, just leave off the "\n" on the last print statement:

    PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "$ ")'
    

    That's equivalent to PS1="\u@\h:\w$ " in BASH:

    qazwart@mybook:~$ cd bin
    qazwart@mybook:~/bin$ cd /usr/local/bin
    qazwart@mybook:/usr/local/bin$ 
    

    It's not quite as easy as setting up a BASH prompt, but you get the idea. Simply write a script for PS1 and Kornshell will execute it.


    For Solaris and other Older Versions of Kornshell

    I found that the above does not work on Solaris. Instead, you'll have to do it the real hackish way...

    • In your .profile, make sure that ENV="$HOME/.kshrc"; export ENV is set. This is probably setup correctly for you.

    • In your .kshrc file, you'll be doing two things

      1. You'll be defining a function called _cd. This function will change to the directory specified, and then set your PS1 variable based upon your pwd.
      2. You'll be setting up an alias cd to run the _cd function.

    This is the relevant part of the .kshrc file:

    function _cd {
       logname=$(logname)   #Or however you can set the login name
       machine=$(hostname)  #Or however you set your host name
       $directory = $1
       $pattern = $2        #For "cd foo bar"
    
       #
       # First cd to the directory
       # We can use "\cd" to evoke the non-alias original version of the cd command
       #
       if [ "$pattern" ]
       then
           \cd "$directory" "$pattern"
       elif [ "$directory" ]
       then
           \cd "$directory"
       else
           \cd
       fi
    
       #
       # Now that we're in the directory, let's set our prompt
       #
    
       $directory=$PWD
       shortname=${directory#$HOME}  #Possible Subdir of $HOME
    
       if [ "$shortName" = "" ]  #This is the HOME directory
       then
            prompt="~$logname"   # Or maybe just "~". Your choice
       elif [ "$shortName" = "$directory" ] #Not a subdir of $HOME
       then
            prompt="$directory"
       else
            prompt="~$shortName"
       fi
       PS1="$logname@$hostname:$prompt$ "  #You put it together the way you like
    }
    
    alias cd="_cd"
    

    This will set your prompt as the equivelent BASH PS1="\u@\h:\w$ ". It isn't pretty, but it works.

提交回复
热议问题