Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Double quote array expansions to avoid re-splitting elements. [SC2068] #437

Open
wwcchh0123 opened this issue Nov 8, 2024 · 0 comments

Comments

@wwcchh0123
Copy link
Contributor

lint 解释

  • 该 lint 的 出现是由于在遍历数组时未使用双引号,可能导致元素被错误地分割成多个部分。在 Bash 中,数组的展开(例如 ${array[@]})需要使用双引号,以确保每个元素被视为一个整体,特别是当元素中包含空格或其他特殊字符时。如果不使用双引号,Shell 可能会将元素错误地分割,从而导致不预期结果。

错误用法

#!/bin/bash

my_array=("Hello World" "Goodbye World")

# 错误的展开
for item in ${my_array[@]}; do
    echo $item
done


# 输出结果
Hello
World
Goodbye
World

正确用法

#!/bin/bash

my_array=("Hello World" "Goodbye World")

# 正确的展开
for item in "${my_array[@]}"; do
    echo "$item"
done


# 输出结果
Hello World
Goodbye World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant