gpt4 book ai didi

regex - 在 shell 中使用转义字符

转载 作者:行者123 更新时间:2023-12-01 11:35:01 26 4
gpt4 key购买 nike

在阅读 shell 中的 Escape characters 时,我尝试了一个示例 -

echo "The balance for user $USER is: \$5:00"

它给出了输出

The balance for user me is: $5.00

但是当我尝试将转义字符 \ 与制表符 \t 或换行符 \n 一起使用时,它似乎并没有工作。我尝试在双引号中使用它(转义字符在双引号中保留其含义)但它仍然不起作用。

echo "The balance for user $USER is:\t\$5:00"

不给出输出

The balance for user me is: $5:00

相反,输出是 -

The balance for user vyadav is:\t$5:00

最佳答案

echo 命令在没有 -e 选项的情况下不会扩展反斜杠转义

您可以使用 echo -e(请注意,这是一个 gnu 扩展,不是很便携):

echo -e "The balance for user $USER is:\t\$5:00"
The balance for user anubhava is: $5:00

根据help echo:

-e  enable interpretation of the following backslash escapes

或者更好地使用 printf:

printf "The balance for user %s is:\t\$%s\n" "$USER" "5:00"

或者

printf 'The balance for user %s is:\t$%s\n' "$USER" "5:00"

The balance for user anubhava is: $5:00

关于regex - 在 shell 中使用转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27949906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com