mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2215 (markdown)
45
SC2215.md
Normal file
45
SC2215.md
Normal file
@@ -0,0 +1,45 @@
|
||||
## This flag is used as a command name. Bad line break or missing `[ .. ]`?
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
if -e .bashrc
|
||||
then
|
||||
echo ".bashrc already exists"
|
||||
fi
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
find . -name '*.mkv'
|
||||
-exec mplayer {} \;
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
if [ -e .bashrc ]
|
||||
then
|
||||
echo ".bashrc already exists"
|
||||
fi
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
find . -name '*.mkv' \
|
||||
-exec mplayer {} \;
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
You are using a name that starts with a dash as a command name. This is almost always a bug.
|
||||
|
||||
There are two typical ways in which this happens:
|
||||
|
||||
* Missing `[ .. ]` or `[[ .. ]]` around a test expression, like in the first example example.
|
||||
* An invalid line break that splits a command in two, like in the second example.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
If you actually have a command that starts with a dash -- which you really reconsider -- you can quote the name (or at least the leading dash). This makes no difference to the shell, but makes it clear to ShellCheck and humans that this is not intended as a flag.
|
Reference in New Issue
Block a user