首页 > 解决方案 > 通过配置PS1在提示符上显示当前时间

问题描述

我正在尝试配置PS1显示当前时间,这是我当前的变量

me@host:~/Downloads$ env | grep -i 'ps1'
PS1=\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 

计划的结果是

me@host; 09:00 AM: ~/Downloads$

\T 以 12 小时格式扩展为当前时间,

临时配置它

me@host:~/Downloads$ export PS1="\[\e]0;\u@\h;\T: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$"

不幸的是,提示没有改变。

我的使用有什么问题?

标签: bash

解决方案


  1. 如果双引号,则变量扩展将删除debian_chroot部分
  2. 字符串中嵌入了两个设置,但您只设置了一个:
    • 第一个(您更改的)设置终端窗口的标题
    • 第二个(您没有更改)设置命令提示符

尝试类似:

PS1='\[\e]0;\u@\h;\T: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\];\T:\[\033[01;34m\]\w\[\033[00m\]\$ '

推荐阅读