mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3028 (markdown)
35
SC3028.md
Normal file
35
SC3028.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
## In POSIX sh, VARIABLE is undefined.
|
||||||
|
|
||||||
|
(or "In dash, ... is not supported." when using `dash`)
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
echo "$HOSTNAME $UID $RANDOM"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
Either switch to a shell like `bash` that supports the special variable you're trying to use, or use an external command to get the information you want:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
echo "$(hostname) $(id -u) $(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null)"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
The variable you are attempting to use is a special variable in bash or ksh. To get the same information from `dash` or POSIX `sh`, use an external command instead.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
If you only intend to target shells that supports this feature, you can change
|
||||||
|
the shebang to a shell that guarantees support, or [[ignore]] this warning.
|
||||||
|
|
||||||
|
You can use `# shellcheck disable=SC3000-SC4000` to ignore all such compatibility
|
||||||
|
warnings.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user