在 printf 中仅转义 ansi 颜色

发布于 2025-01-20 05:45:41 字数 221 浏览 2 评论 0原文

我想用 ansi 颜色在控制台窗口路径中打印。

像这样:

file=read -r

printf "\033[32+++Path is \\\\uncshare\\testpath\\$file++\033[0m"

我需要转义任何特殊符号,例如\n,因为如果有人输入nested.txt作为文件名,例如,它将被解释为新行,但我想保留ansi颜色。

I want to print in console windows path with ansi color.

Like this:

file=read -r

printf "\033[32+++Path is \\\\uncshare\\testpath\\$file++\033[0m"

I need to escape any special symbol like \n, because if somebody type nested.txt as a name of the file, for example, it will be interpreted as new line,but i want to preserve ansi color.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-01-27 05:45:41

printf 根据格式说明符替换字符串,然后您可以执行类似的操作

printf '\033[32+++Path is \\\\uncshare\\testpath\\%s++\033[0m' "$file"

以避免解释 file 中的字符。

printf replaces strings based on format specifiers, then you can do something like

printf '\033[32+++Path is \\\\uncshare\\testpath\\%s++\033[0m' "$file"

to avoid characters in file being interpreted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文