提问人:user16965639 提问时间:7/12/2022 最后编辑:Peter Mortensenuser16965639 更新时间:8/22/2023 访问量:191
如何在 Bash 中转义单引号和双引号
How to escape single and double quotes in Bash
问:
我有一个脚本,将文本作为参数并对其进行编码。如果文本只包含单引号,我会用双引号将其括起来,反之亦然,但如果它同时包含两个引号,就会变得棘手,因为它会在第一次出现时被关闭。
script "I can't use "test" text"
// Double quote will be terminated at "I can't use "
那么,如何在不使用反斜杠的情况下对这两种情况进行转义,就像在 Python 中使用三重单引号或双引号一样。
script '''I can't use "test" text'''
// or
script """I can't use "test" text"""
答:
1赞
Barmar
7/12/2022
#1
使用 here-document,并使用命令 subsitution 将其转换为参数。
script "$(cat <<'EOF'
I can't use "test" text
EOF)"
或
评论
echo "I can't use "test" text"
test