【问题标题】:How to get shell PROMPT? [closed]如何获得外壳提示? [关闭]
【发布时间】:2021-06-08 03:41:38
【问题描述】:

我们可以使用echo $PS1查看

test@testauto:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
test@testauto:~$

但我想要的是 "test@testauto:~$" 而不是 "[\e]0;\u@\h: \w\a]${debian_chroot:+($debian_chroot)}\u@\h :\w$"

我怎样才能得到它?

【问题讨论】:

  • 顺便说一句,我总是发现"\[\e[38;5;244m\]\D{%R}\[\e[38;5;32m\] \h:\w> \[\e[0m\]" 的用户提示和"\[\e[1;34m\][\[\e[38;5;203m\]\A \[\e[1;34m\]\h\[\e[38;5;197m\]:\w\[\e[1;34m\]] # \[\e[0m\]" 的根提示很有帮助。 (256色xterm)除了提供时间之外,他们还提供了当前路径信息的形式,可以和rsyncscp等一起使用。 00:48 wizard:~/tmpd> 的用户提示和 root 提示(相同位置)[00:48 wizard:/home/david/tmpd] #
  • @GavinHunte21 :您将问题标记为 shell,即 POSIX shell。请参阅 here 了解您可以在 PS1 中添加的内容。我不清楚您的问题如何符合 POSIX shell 的概念。

标签: linux shell


【解决方案1】:

对于 Bash

echo "${PS1@P}"

对于 ZSH

echo "${(%%)PS1}"

您可以在此线程中找到更多相关信息:How to print current bash prompt?

【讨论】:

  • 我在 Ubuntu 上测试过,结果符合预期。我在使用busybox的嵌入式设备上对其进行了测试,但失败了。 test_aut:~ # echo "${PS1@P}" /bin/sh: syntax error: bad substitution test_aut:~ # ls -al /bin/sh lrwxrwxrwx 1 root 0 7 May 25 05:14 /bin/sh -> busybox 有没有cmets?提前致谢。
  • @GavinHunter21 这是因为busybox没有使用bash或zsh。根据这个线程serverfault.com/questions/241959/…它的一些灰分衍生物。我对busybox不熟悉,但是在查看了代码后我不确定它是否支持。
猜你喜欢
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
相关资源
最近更新 更多