Keywords being used as arguments Medium

Found keywords like 'done', 'then', 'fi', and 'esac' used as arguments in commands, causing the shell to treat them as literal strings instead of keywords. For example, in echo '$f' done, the keyword done is not recognized as terminating the loop. This issue is fixed by terminating the preceding command with a semicolon, ensuring the keyword starts a new line.

Detector ID
shell/keywords-being-used-as-arguments@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: `fi` is treated as an argument to echo, not terminating the `if` statement.
3if [ -f "file.txt" ]
4then
5  echo "File exists" fi
6  echo "This always runs"

Compliant example

1
2# Compliant: `fi` properly terminates the `if` statement.
3if [ -f "file.txt" ]
4then
5  echo "File exists"
6fi
7echo "This runs after the if statement"