Linux 命令提示符显示全路径prompt配置

2025-11-05 12:21:06

1、用下面的命令输出查看shell版本,不同的shell版本对应有不同的配置方法。

echo $SHELL

1、Bash shell配置命令提示符的变量是PS1,同时它还提供了一些特殊的变量来支持提示符的设置,所以只需要根据需要配置PS1的值即可实现Shell命令提示符的更改。

\d :代表日期,格式为 Weekday Month Date,例如 "Mon Aug 1"

\H :完整的主机名称。

\h :仅取主机名称的第一个名字。如下图所示

\t :显示时间,为 24 小时格式。

\T :显示时间,12 小时的时间格式。

\A :显示时间,24 小时格式, HH:MM

\u :目前使用者的账号名称;

\v :BASH 的版本信息;

\w :完整的工作目录名称。家目录会以 ~ 取代;

\W :利用 basename 取得工作目录名称,所以仅会列出最后一个目录名。

\# :下达的第几个指令。

\$ :提示字符,如果是 root 时,提示字符为 # ,否则就是 $ 。

例如:

>> PS1='\w >>'

~ >>

~ >>

具体结果如下图所示:

Linux 命令提示符显示全路径prompt配置

1、TCshell 配置命令提示符的变量是prompt,同时它也提供了一些特殊的变量来支持提示符的设置,所以我们只需要更改prompt变量的值来改命令提示符。

%/  The current working directory. 

%~  The  current working directory, but with one's

   home directory represented by  `~'  and  other

   users' home directories represented by `~user'

   as per Filename substitution.  `~user' substi-

   tution  happens  only if the shell has already

   used `~user' in a pathname in the current ses-

   sion.

%c[[0]n], %.[[0]n]

   The  trailing component of the current working

   directory, or n trailing components if a digit

   n  is given.  If n begins with `0', the number

   of skipped  components  precede  the  trailing

   component(s)  in  the format `/<skipped>trail-

   ing'.  If the ellipsis shell variable is  set,

   skipped   components  are  represented  by  an

   ellipsis so the whole  becomes  `...trailing'.

   `~' substitution is done as in `%~' above, but

   the `~' component  is  ignored  when  counting

   trailing components.

%C  Like %c, but without `~' substitution.

%M  The full hostname (e.g. jaguar.apple.com).

%m  The hostname up to the first `.' (e.g. jaguar).

%n  The user name.

例:

>> set prompt="%~ >> "

~ >>

~ >> 

~ >> set prompt="%/ >> "

/home/guest >>

/home/guest >>

具体示例如下图

Linux 命令提示符显示全路径prompt配置

1、Kshell配置命令提示符的变量是PS1, 它把提示符变量PS1作为一个特殊的变量看待,所以我们只需要更改这个变量就可以完成对命令提示符的配置。

>> PS1="$PWD >>"

/home/guest/script >> 

/home/guest/script >>

Linux 命令提示符显示全路径prompt配置

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢