mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Revert a89b80409e1050fcdc4ca8772acfa2e52853cf43...33087384aec8dcc93f0dca781770f579d768f6f9 on Template
33
Template.md
33
Template.md
@@ -3,43 +3,18 @@
|
|||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
#!/bin/sh
|
# Simple example of problematic code
|
||||||
myfunction
|
|
||||||
|
|
||||||
myfunction() {
|
|
||||||
echo "Hello World"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
#!/bin/sh
|
# Simple example of above code, only fixed
|
||||||
myfunction() {
|
|
||||||
echo "Hello World"
|
|
||||||
}
|
|
||||||
myfunction
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
You are calling a function that you are defining later in the file. The function definition must come first.
|
(An explanation of why the code is problematic and how the correct code is an improvement)
|
||||||
|
|
||||||
Function definitions are much like variable assignments, and define a name at the point the definition is "executed". This is why they must happen before their first use.
|
|
||||||
|
|
||||||
This is especially apparent when defining functions conditionally:
|
|
||||||
|
|
||||||
```
|
|
||||||
case "$(uname -s)" in
|
|
||||||
Linux) hi() { echo "Hello from Linux"; } ;;
|
|
||||||
Darwin) hi() { echo "Hello from macOS"; } ;;
|
|
||||||
*) hi() { echo "Hello from something else"; } ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
hi
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Exceptions:
|
### Exceptions:
|
||||||
|
|
||||||
None.
|
(Cases where the user may choose to ignore this warning, if any.)
|
Reference in New Issue
Block a user