Incorrect Conditional Syntax Medium

The script uses 'else if' for conditional branching, which is not the correct syntax in shell scripting. Use 'elif' instead of 'else if' to properly structure conditional statements in shell scripts.

Detector ID
shell/incorrect-conditional-syntax@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: Uses `else if` which is not valid shell syntax for alternative conditions.
3if [ "$fruit" = "apple" ]
4then
5    echo "It's an apple"
6else if [ "$fruit" = "banana" ]
7then
8    echo "It's a banana"
9else
10    echo "It's something else"
11fi fi

Compliant example

1
2# Compliant: Uses `elif` for proper alternative condition checking in shell scripts.
3if [ "$fruit" = "apple" ]
4then
5    echo "It's an apple"
6elif [ "$fruit" = "banana" ]
7then
8    echo "It's a banana"
9else
10    echo "It's something else"
11fi