gpt4 book ai didi

shell - printf 格式字符串中的变量插值与替换

转载 作者:行者123 更新时间:2023-12-03 22:45:38 24 4
gpt4 key购买 nike

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

是:

printf "$file does not exist\n"

在某些方面不如:
printf "%s does not exist\n" "$file"

最佳答案

因为理论上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

关于shell - printf 格式字符串中的变量插值与替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com