Created SC2268 (markdown)

Vidar Holen
2020-10-19 19:35:50 -07:00
parent 4462db0511
commit 9bd47eb4da

47
SC2268.md Normal file

@@ -0,0 +1,47 @@
## Avoid outdated x-prefix in comparisons as it no longer serves a purpose.
### Problematic code:
```sh
[ "x$pass" = "xswordfish" ]
test x"$var" = x
```
### Correct code:
```sh
[ "$pass" = "swordfish" ]
test "$var" = ""
```
### Rationale:
Some older shells would get confused if the first argument started with a dash, or consisted of `!` or `(`. As a workaround, people would prefix variables and values to be compared with `x` to ensure the left-hand side always started with an alphanumeric character.
POSIX ensures [this is not necessary](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html), and all modern shells now follow suite.
### Examples:
Bash 1.14 from 1992 incorrectly fails this test. This was fixed for Bash 2.0 in 1996:
var='!'
[ "$var" = "!" ]
Dash 0.5.4 from 2007 incorrectly passes this test. This was fixed for Dash 0.5.5 in 2008:
var='('
[ "$var" = ")" ]
Zsh (while not supported by ShellCheck) fixed the same problem in 2015.
### Exceptions:
This warning is optional, and not enabled by default.
If you are targeting especially old shells, you should not enable this warning.
### Related resources:
* [Wooledge Bash Pitfall #4](https://mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D)