AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1310665
Accepted
Gabriel Staples
Gabriel Staples
Asked: 2021-01-25 11:28:31 +0800 CST2021-01-25 11:28:31 +0800 CST 2021-01-25 11:28:31 +0800 CST

如何在 PS1 提示字符串中使用 sed 替换终端标题

  • 772

我已经玩了> 1小时。并且无法弄清楚这一点。这是我的PS1提示字符串:

$ echo $PS1
\$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;this is the title\a\]

终端窗口标题是this is the title部分。\[\e]2;标记标题的开始,并\a\]标记结束。请参阅此处的“自定义终端窗口标题”。

根据https://regex101.com/,正则表达式字符串\\\[\\e\]2;.*\\a\\\]似乎与标题及其转义字符匹配。在此处查看它的演示:https ://regex101.com/r/okcx0T/1 。

因此,该sed命令应该从字符串中删除标题PS1并打印输出,但它似乎与标题字符串完全不匹配!从 中可以看出echo $PS1,没有区别:

$ echo $PS1 | sed -E "s|\\\[\\e\]2;.*\\a\\\]||"
\$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;this is the title\a\]

$ echo $PS1
\$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;this is the title\a\]

如何替换PS1变量中的这个标题字符串,有或没有sed?

我的sed命令显然适用于简单的匹配。看这个!从提示字符串this is the title的末尾明显删除。PS1

$ echo $PS1 | sed 's|this is the title||'
\$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;\a\]

我似乎无法匹配标题字符串的开头和结尾转义字符就是全部!

我在尝试解决这个问题时搜索过的参考资料

  1. https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
  2. https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/
  3. https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash
  4. https://superuser.com/questions/1107680/how-to-use-sed-with-piping
  5. https://stackoverflow.com/questions/8356958/sed-just-trying-to-remove-a-substring
  6. https://unix.stackexchange.com/questions/169207/remove-backslashes-from-a-text-file/169210#169210' - 有一些关于在 bash、 vs"等中转义的重要评论。
  7. https://unix.stackexchange.com/questions/19491/how-to-specify-characters-using-hexadecimal-codes-in-grep
  8. https://en.wikipedia.org/wiki/ASCII
bash
  • 1 1 个回答
  • 395 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2021-01-25T11:47:34+08:002021-01-25T11:47:34+08:00

    您的问题是双引号允许外壳替换\\为\- 如果您使用外壳-x选项运行命令,您可以看到这一点:

    $ set -x
    
    $ echo foo | sed -E "s|\\\[\\e\]2;.*\\a\\\]||"
    + echo foo
    + sed -E 's|\\[\e\]2;.*\a\\]||'
    foo
    

    如果您更改为 sed 表达式周围的单引号,它应该可以工作。该-E标志不是必需的。给定

    $ printf '%s\n'  "$ps1"
    \$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;this is the title\a\]
    

    然后

    $ set -x
    
    $ printf '%s\n' "$ps1" | sed 's|\\\[\\e\]2;.*\\a\\\]||'
    + sed 's|\\\[\\e\]2;.*\\a\\\]||'
    + printf '%s\n' '\$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]2;this is the title\a\]'
    \$SHLVL:2 \e[7m$(gs_git_show_branch)\e[m\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
    
    • 1

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve