好吧,有点老了,有点晚了,但这是我在 Kornshell 中使用的:
PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
这会产生一个相当于 BASH 中的 PS1="\u@\h:\w\n$ " 的提示。
例如:
qazwart@mybook:~
$ cd bin
qazwart@mybook:~/bin
$ cd /usr/local/bin
qazwart@mybook:/usr/local/bin
$
我喜欢两行提示,因为有时我的目录名很长,而且会占用大量命令行。如果你想要一个单行提示,只需在最后一个打印语句中去掉“\n”:
PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "$ ")'
这相当于 BASH 中的PS1="\u@\h:\w$ ":
qazwart@mybook:~$ cd bin
qazwart@mybook:~/bin$ cd /usr/local/bin
qazwart@mybook:/usr/local/bin$
这并不像设置 BASH 提示那么简单,但你明白了。只需为PS1 编写一个脚本,Kornshell 就会执行它。
适用于 Solaris 和其他旧版 Kornshell
我发现上述方法在 Solaris 上不起作用。相反,你必须以真正的黑客方式来做......
这是.kshrc文件的相关部分:
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"
这会将您的提示设置为等效的 BASH PS1="\u@\h:\w$ "。它不漂亮,但很有效。