[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 系统管理 > 其它 > 正文

要怎么设定 prompt 才会显示出目前所在的目录?

来源:Steve Hayman (2001-04-19 10:26:54)

这得视你的 shell 而定。有些 shell 很容易,有些 shell 很难,有些根 
本办不到。 

C Shell (csh): 

  将以下的东西加入你的 .cshrc 里。 

        alias setprompt 'set prompt="${cwd}% "' 
        setprompt           # to set the initial prompt 
        alias cd 'chdir \!* && setprompt' 

  假如你有用 pushd 与 popd, 把底下的东西也加进去。 

        alias pushd 'pushd \!* && setprompt' 
        alias popd  'popd  \!* && setprompt' 

  若你的 C shell 没有 $cwd 这个变数,那就得用 `pwd` 代替之。 

  若你想要的只是 prompt 里有目前所在目录的最后一个成分 
  ("mail%" 而?nbsp;"/usr/spool/mail%") 则用 

        alias setprompt 'set prompt="$cwd:t% "' 

  有些旧版的 csh 将 && 和 || 的意义弄反了。你可以试试看: 

        false && echo bug 

  若结果是印出 "bug",那就把 && 和 || 对调,或找一个没有这种 
  bug 的 csh 来用。 

Bourn Shell (sh): 

  如果你有较新版的 Bourn Shell(SVR2 或更新的版本),那么你就可 
  以用一个 shell function 来造你自己的命令,譬如 "xcd": 

        xcd() { cd $* ; PS1="`pwd` $ ";} 

  如果你的 Bourn Shell 是比较旧的版本,也是可以做到,但是方法比 
  较复杂。这里提供一个方法。把以下的内容加入你的 .profile: 

        LOGIN_SHELL=$$ export LOGIN_SHELL 
        CMDFILE=/tmp/cd.$$ export CMDFILE 
        # 16 is SIGURG, pick a signal that's not likely to be 
        used 
        PROMPTSIG=16 export PROMPTSIG 
        trap '. $CMDFILE' $PROMPTSIG 

  然后把以下的部份写成一个可执行的 script(不需要缩排),名字就 
  叫做 "xcd",放在你的 PATH 中 

        : xcd directory - change directory and set prompt 
        : by signalling the login shell to read a command file 

        cat >${CMDFILE?"not set"} <        cd $1 
        PS1="\`pwd\`$" 
        EOF 
        kill -${PROMPTSIG?"not set"} ${LOGIN_SHELL?"not set"} 

  那么,现在就可已用 "xcd /some/dir" 来改变工作目录了。 

Korn Shell (ksh): 

  把下面这行加入你的 .profile 中: 

        PS1='$PWD $ ' 

  如果你只想显示最后一个部分,那么就用 

        PS1='${PWD##*/} $ ' 

T C shell (tcsh) 

  Tcsh 是常用的 csh 加强版,增加了一些内建变数(及许多其他的功 
  能): 

        %~          the current directory, using ~ for $HOME 
        %/          the full pathname of the current directory 
        %c or %.    the trailing component of the current directory 

  所以你可以直接用 

        set prompt='%~' 

BASH (FSF's "Bourne Again Shell") 

  $PS1 中的 \w 表示工作目录的完整路径(以 ~ 表示 $HOME);\W 则 
  是表示工作目录的最后一个部份。所以,只要把前面所提有关 sh 和 
  ksh 的方法做以下的修改 

        PS1='\w $ ' 
  或 
        PS1='\W $ ' 
(http://www.fanqiang.com)



 
 相关文章

★  感谢所有的作者为我们学习技术知识提供了一条捷径  ★
www.fanqiang.com