dav*_*lab 11 unix shell customization ksh environment-variables
我在Solaris上使用KornShell(ksh),目前我的PS1 env var是:
PS1="${HOSTNAME}:\${PWD} \$ "
并显示提示: hostname:/full/path/to/current/directory $
但是,我希望它显示: hostname:directory $
换句话说,我怎么能只显示主机名和当前目录,即名称tmp或~或public_html等等等等?
Dav*_* W. 11
好吧,有点老了,有点晚了,但这就是我在Kornshell中使用的:
PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
Run Code Online (Sandbox Code Playgroud)
这会产生一个与PS1="\u@\h:\w\n$ "BASH 相同的提示.
例如:
qazwart@mybook:~
$ cd bin
qazwart@mybook:~/bin
$ cd /usr/local/bin
qazwart@mybook:/usr/local/bin
$
Run Code Online (Sandbox Code Playgroud)
我喜欢两行提示,因为我有时会有很长的目录名,而且它们占用了很多命令行.如果您想要一行提示,请在最后一个打印语句中取消"\n":
PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "$ ")'
Run Code Online (Sandbox Code Playgroud)
这相当于PS1="\u@\h:\w$ "BASH:
qazwart@mybook:~$ cd bin
qazwart@mybook:~/bin$ cd /usr/local/bin
qazwart@mybook:/usr/local/bin$
Run Code Online (Sandbox Code Playgroud)
它不像设置BASH提示那么容易,但你明白了.只需编写一个脚本PS1,Kornshell就会执行它.
我发现上面的内容在Solaris上不起作用.相反,你必须以真正的hackish方式做到这一点......
在你的.profile,确保ENV="$HOME/.kshrc"; export ENV
设置.这可能是你正确设置的.
在你的.kshrc文件中,你将做两件事
_cd.此函数将更改为指定的目录,然后根据您的pwd设置PS1变量.cd来运行该_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"
Run Code Online (Sandbox Code Playgroud)
这会将您的提示设置为等效的BASH PS1="\u@\h:\w$ ".它不漂亮,但它的工作原理.
| 归档时间: |
|
| 查看次数: |
74740 次 |
| 最近记录: |