如何在 Bash 中转义单引号和双引号

问题描述 投票:0回答:1

我有一个脚本,它将文本作为参数并对其进行编码。如果文本仅包含单引号,我会用双引号将其括起来,反之亦然,但如果它包含两者,则会变得很棘手,因为它会在第一次出现时关闭。

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"""
bash escaping quotes double-quotes single-quotes
1个回答
1
投票

使用此处文档,并使用命令替换将其变成参数。

script "$(cat <<'EOF'
I can't use "test" text
EOF)"

或者

© www.soinside.com 2019 - 2024. All rights reserved.