Created SC3054 (markdown)

Vidar Holen
2020-09-01 17:30:45 -07:00
parent d2c5b1c903
commit f1ff88bdb0

45
SC3054.md Normal file

@@ -0,0 +1,45 @@
## In POSIX sh, array references are undefined.
(or "In dash, ... are not supported." when using `dash`)
### Problematic code:
```sh
#!/bin/sh
var=(foo bar)
echo "${var[1]}"
```
### Correct code:
The easiest solution is to switch to a shell that does support arrays, like `bash`:
```sh
#!/bin/bash
var=(foo bar)
echo "${var[1]}"
```
Alternatively, rewrite the logic to use e.g. indirect variable references or `set`:
```sh
#!/bin/sh
set -- foo bar
echo "$2"
```
### Rationale:
Arrays are supported in `bash` and `ksh`, but not in `dash` or POSIX `sh`. Either switch to a shell that supports them, or rewrite your script without relying on arrays. This may not be straight forward.
### 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!