假设我有一个脚本,可以打印出传递给它的参数数量:
# file: num_args
echo "Number of arguments: $#"
现在我的问题与以下调用有关:
> ./num_args a b c
> Number of arguments: 3 # As I would expect.
> ./num_args "a b c"
> Number of arguments: 1 # As I would expect.
> ./num_args a\ b\ c
> Number of arguments: 1 # As I would expect.
> printf "%q\n" "a b c"
> a\ b\ c # As I would expect.
> ./num_args $(printf "%q" "a b c")
> Number of arguments: 3 # NOT as I would expect.
鉴于printf
手册页指出了这一点
%q ARGUMENT is printed in a format that can be reused as shell input, escaping non-printable characters with the proposed POSIX $'' syntax.
我不确定在以上最后一种情况下会发生什么。
答案 0 :(得分:0)
printf %q
输出被正确地转义,可以被外壳解释器解析为源代码。
但是,未引用的扩展名不会被解析为源代码。它们仅经历字符串拆分和glob扩展阶段。 (这是一件好事:否则,在shell脚本中处理不受信任的文件名或其他潜在危险的内容几乎是不可能的。)
您可以将此输出替换为shell命令,例如sh -c "...$foo..."
或eval "...$foo..."
或ssh somehost "...$foo..."
;它不能(不应该)不加引号使用。
一些重要的参考文献:
eval
(上面讨论的解决方法系列之一)所涉及的风险。