5

如果将变量放入printf(1)格式字符串中, ShellCheck会发出警告。为什么?

是:

printf "$file does not exist\n"

在某些方面不如:

printf "%s does not exist\n" "$file"
4

1 回答 1

6

因为理论上file变量可以有一些格式字符会失败printf。这些例子会更清楚:

file='my'
printf "$file does not exist\n"
my does not exist    

file='m%y'
printf "$file does not exist\n"
-bash: printf: `y': invalid format character

根据建议,它将正常工作:

printf "%s does not exist\n" "$file"
m%y does not exist
于 2015-09-18T10:53:36.623 回答