shell 命令提示符中顯示時間(qbit)

qbit發表於2023-02-16

前言

  • 本文對 Ubuntu 20.04 適用

bash

  • bash 版本 5.0.17
  • 配置

    export PS1="\u@\h: \$PWD/ \D{%Y-%m-%d} \t\n$ "
  • 結果

    qbit@qhost: /home/qbit/ 2023-02-16 17:13:22
    $ date
    Thu 16 Feb 2023 05:13:23 PM CST
  • 可以將上述配置加到 .bashrc

fish

  • 編輯檔案 ~/.config/fish/functions/fish_prompt.fish (目錄或檔案不存在就新建),模板參照 /usr/share/fish/functions/fish_prompt.fish

    function fish_prompt
       if not set -q __fish_prompt_hostname
          set -g __fish_prompt_hostname (hostname)
       end
       set_color -o cyan
       echo -n -s "$USER" @ "$__fish_prompt_hostname" ": "
       set_color -o green
       echo -n (prompt_pwd)
       echo -n ' ' 
       echo -n (date +"%Y-%m-%d %H:%M:%S"  )
       echo ''
       echo -n "\$ "
       set_color normal
    end
  • fish 的 ~/.config/fish/config.fish 對標 bash 的 .bashrc
  • 編輯 ~/.config/fish/config.fish 顯示完整路徑

    set -U fish_prompt_pwd_dir_length 0
  • 效果如下

    qbit@qhost: /data/mongo/db 2023-02-16 17:59:00
    $ date
    Thu 16 Feb 2023 05:59:02 PM CST
本文出自 qbit snap

相關文章