Reputation: 10310
I am using KornShell (ksh) on Solaris and currently my PS1 env var is:
PS1="${HOSTNAME}:\${PWD} \$ "
And the prompt displays: hostname:/full/path/to/current/directory $
However, I would like it to display: hostname:directory $
In other words, how can I display just the hostname and the name of the current directory, i.e. tmp
or ~
or public_html
etc etc?
Upvotes: 14
Views: 91029
Reputation: 193
So,
PS1=`id -un`@`hostname -s`:`print $PWD | sed "s,$(print $HOME),~,"`$' '
in ~/.profile should work on all versions of the Korn shell, but there's a catch: the PS1 variable is set at login on ksh and does not get updated with a change directory (cd) command.
You will need to add an entry to ~/.profile along the lines of
chdir ()
{
\cd ${*:-$HOME} && PS1=`id -un`@`hostname -s`:`print $PWD | sed "s,$(print $HOME),~,"`$' '
}
alias cd=chdir
cd
to get PS1 to update with cd commands (and, obviously, if one should somehow change directories by some other means, PS1 will not update and YA kluge will be necessary).
Upvotes: 0
Reputation: 1
and...
if you work between two shells for most of your effort [ksh and bourne sh] and desire a directory tracking display on your command line then PWD can be substituted easily in ksh and if you use /usr/xpg4/bin/sh for your sh SHELL, it will work there as well
Upvotes: -2
Reputation: 107040
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.
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
_cd
. This function will change to the directory specified, and then set your PS1 variable based upon your pwd.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.
Upvotes: 14
Reputation: 19
HOST=`hostname`
PS1='$(print -n "[${USER}@${HOST%%.*} ";[[ "$HOME" == "$PWD" ]] && print -n "~" ||([[ "${PWD##*/}" == "" ]] && print -n "/" || print -n "${PWD##*/}");print "]$")'
Upvotes: 1
Reputation: 39
ENV=~/.kshrc, and then in your .kshrc:
function _cd {
\cd "$@"
PS1=$(
print -n "$LOGNAME@$HOSTNAME:"
if [[ "${PWD#$HOME}" != "$PWD" ]]; then
print -n "~${PWD#$HOME}"
else
print -n "$PWD"
fi
print "$ "
)
}
alias cd=_cd
cd "$PWD"
Brad
Upvotes: 3
Reputation: 351
From reading the ksh man page you want
PS1="${HOSTNAME}:\${PWD##*/} \$ "
Tested on default ksh on SunOS 5.8
Upvotes: 22
Reputation: 3274
Try this:
PS1="\H:\W"
More information on: How to: Change / Setup bash custom prompt, I know you said ksh, but I am pretty sure it will work.
Upvotes: -3