bash 打印转义文件内容

bash print escaped file contents

我正在尝试使用转义双引号打印文件内容。

# read file contents from ${filename}
# - escape double quotes
# - represent newlines as '\n' 
# print the result
echo "my file contents: \"${out}\""

例如,如果我的文件是

<empty line>
console.log("hello, world");
<empty line>

它应该打印

my file contents: "\nconsole.log(\"hello, world\");\n"

我试图将 printf 与 %q 格式说明符一起使用,但遇到了删除尾随空格的问题。

命令替换去除尾随换行符。您可以通过添加一个虚拟的非换行符然后删除它来防止这种情况:

printf '\n\nfoo\n\n' > file

contents="$(cat "file"; printf x)"
contents="${contents%x}"

printf "The shell equivalent of the file contents is: %q\n" "$contents"

如果您尝试生成 JSON,您应该改为使用 jq

只执行您明确要求的两个文字转换:

IFS= read -r -d '' content <file
content=${content//'"'/'\"'/}
content=${content//$'\n'/'\n'}
echo "file contents: $content"

就是说,如果您试图将任意内容表示为 JSON 字符串,让完全兼容的 JSON parser/generator 来完成繁重的工作:

IFS= read -r -d '' content <file
echo "file contents: $(jq -n --arg content "$content" '$content')"

...或者,甚至更好(甚至支持内容 bash 无法存储为字符串的文件),让 jq 直接从输入文件中读取:

echo "file contents: $(jq -Rs . <file)"

在我看来,将任意多行文本转换为 printf 格式的最可靠方法是使用内置于 bash.

中的 printf
$ nl -ba testfile
     1
     2  console.log("hello, world");
     3
$ s="$(printf '%q' "$(cat testfile; printf x)")"
$ s="${s%x\'}"; s="${s#$\'}"
$ echo "$s"
\nconsole.log("hello, world");\n\n

这具有处理 所有 个字符的优势,包括 CR 和制表符而不仅仅是换行符。

请注意我们使用有趣的命令扩展解决方法来避免删除尾随换行符。 (否则,我们可以 s="$(printf '%q' "$(<testfile)")"。)

另请注意我们在 echo 之前的行中进行的参数扩展。这是必需的,因为 bash 处理 %q 格式字符的方式,返回格式引用的字符串,而不仅仅是格式化的字符串。