Предположим, у меня есть скрипт, который выводит количество аргументов, переданных ему:
# 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
Страница man утверждает, что
%q ARGUMENT is printed in a format that can be reused as shell input, escaping non-printable characters with the proposed POSIX $'' syntax.
Я не уверен, что произойдет в последнем случае выше.