Unix 如何在KornShell中自定义显示提示以显示主机名和当前目录?
我正在Solaris上使用KornShell(ksh),目前我的PS1环境变量为:Unix 如何在KornShell中自定义显示提示以显示主机名和当前目录?,unix,shell,environment-variables,customization,ksh,Unix,Shell,Environment Variables,Customization,Ksh,我正在Solaris上使用KornShell(ksh),目前我的PS1环境变量为: PS1=“${HOSTNAME}:\${PWD}\$” 提示显示:hostname:/full/path/to/current/directory$ 但是,我希望它显示:hostname:directory$ 换句话说,如何仅显示当前目录的主机名和名称,即tmp或~或public\u html等?尝试以下操作: PS1="\H:\W" 更多信息:,我知道你说的是ksh,但我很肯定它会起作用。阅读你想要的 PS
PS1=“${HOSTNAME}:\${PWD}\$”
提示显示:hostname:/full/path/to/current/directory$
但是,我希望它显示:hostname:directory$
换句话说,如何仅显示当前目录的主机名和名称,即tmp
或~
或public\u html
等?尝试以下操作:
PS1="\H:\W"
更多信息:,我知道你说的是ksh,但我很肯定它会起作用。阅读你想要的
PS1=“${HOSTNAME}:\${PWD##*/}\$”
在SunOS 5.8上的默认ksh上测试好的,有点旧,有点晚,但这是我在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上不起作用。相反,你必须用真正的黑客方式
- 在
中,确保.profile
一切就绪。这可能是为您正确设置的ENV=“$HOME/.kshrc”;出口环境
- 在
文件中,您将做两件事.kshrc
- 您将定义一个名为
的函数。此函数将更改为指定的目录,然后根据pwd设置PS1变量\u cd
- 您将设置一个别名
,以运行cd
功能\u cd
.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$”
。它虽然不漂亮,但很有效。ENV=~/.kshrc,然后在您的.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"
布拉德和
如果你的大部分精力都在两个shell之间工作[ksh和bourne sh]
并希望在命令行上显示目录跟踪
然后,PWD可以在ksh中轻松替换
如果您在sh SHELL中使用/usr/xpg4/bin/sh,它也可以在那里工作对不起,它不能像您键入的那样工作。我很确定您键入的是BASH语法。谢谢。这个提示行在OpenBSD6ksh上或多或少产生了所需的结果。我自己也在使用PS1=“\h:\W”。在ksh上也适用于我。我很好奇##*/是否是一个常见的技巧,并且找到了这个网站。它还有一些其他有用的shell技巧;)可以它已被添加。谢谢:PS1略短一些,它是$(print-n“
logname
主机名:”[“${PWD#$HOME}”!=“$PWD”]]和print-n“~${PWD#$HOME}”| print-n“$PWD;print”\n$”,不鼓励只回答代码问题,因为它们没有解释如何解决问题。请更新您的答案,以解释这是如何改进此问题已有的其他被接受和投票表决的答案的。另外,这个问题已经问了8年了,您的努力会得到最近有未回答问题的用户的赞赏。请复习。
PS1=`id -un`@`hostname -s`:'$PWD'$
HOST=`hostname`
PS1='$(print -n "[${USER}@${HOST%%.*} ";[[ "$HOME" == "$PWD" ]] && print -n "~" ||([[ "${PWD##*/}" == "" ]] && print -n "/" || print -n "${PWD##*/}");print "]$")'
PS1=`id -un`@`hostname -s`:'$PWD'$