mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC1075 (markdown)
46
SC1075.md
Normal file
46
SC1075.md
Normal file
@@ -0,0 +1,46 @@
|
||||
## Use 'elif' instead of 'else if'.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
if [ "$#" -eq 0 ]
|
||||
then
|
||||
echo "Usage: ..."
|
||||
else if [ "$#" -lt 2 ]
|
||||
then
|
||||
echo "Missing operand"
|
||||
fi
|
||||
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
if [ "$#" -eq 0 ]
|
||||
then
|
||||
echo "Usage: ..."
|
||||
elif [ "$#" -lt 2 ]
|
||||
then
|
||||
echo "Missing operand"
|
||||
fi
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
Many languages allow alternate branches with `else if`, but `sh` is not one of them. Use `elif` instead.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
`else if` is a valid (though confusing) way of nesting an `if` statement in a parent's `else`. If this is your intention, please use canonical formatting and put a linefeed between `else` and `if`.
|
||||
|
||||
```sh
|
||||
|
||||
if x
|
||||
then
|
||||
echo "x"
|
||||
else # line break here
|
||||
if y
|
||||
then
|
||||
echo "y"
|
||||
fi
|
||||
fi
|
||||
```
|
Reference in New Issue
Block a user