note: Double quote to prevent globbing and word splitting. [SC2086] #384
CarlJi
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
这个提示是来自
ShellCheck
工具,它是一个用于检查 shell 脚本的静态分析工具。SC2086
是其中的一个警告代码,解释如下:问题描述:
SC2086: Double quote to prevent globbing and word splitting.
意思是:应该使用双引号包裹变量,以避免文件名通配符(globbing)和单词分割(word splitting)的问题。如果在 shell 脚本中未对变量进行适当的双引号处理,shell 可能会错误地处理某些字符,导致意外的行为。
具体场景:
假设你有以下代码:
rm $file
这里的
$file
没有加双引号。如果$file
包含空格或特殊字符,shell 会将其拆分成多个部分。例如,如果$file
是"my file.txt"
,shell 会将它拆分为my
和file.txt
,并且rm
命令可能会报错,或者删除错误的文件。此外,如果
$file
包含通配符(如*
),shell 可能会展开这个通配符,误操作多个文件。正确的做法:
使用双引号包裹变量,确保 shell 处理整个变量值作为一个整体,而不是分割它或解析通配符。
rm "$file"
这样,即使
$file
包含空格或特殊字符,rm
仍会正常处理整个文件名。总结:
SC2086
警告提醒你,应该对变量使用双引号,防止:*
,?
等)。通过加双引号,可以避免这些潜在的错误。
Beta Was this translation helpful? Give feedback.
All reactions