mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3003 (markdown)
49
SC3003.md
Normal file
49
SC3003.md
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
## In POSIX sh, $'..' is undefined.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
IFS=$' \t\n'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
# Note: \n can not be last, or it will be stripped by $()
|
||||||
|
IFS=$(printf ' \n\t')
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
# Trailing linefeed added literally
|
||||||
|
IFS="$(printf ' \t')
|
||||||
|
"
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Bash supports this
|
||||||
|
IFS=$' \t\n'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You are using the interpolated string Bashism `$'..'` in a script that declares itself as POSIX sh (e.g. via `#!/bin/sh`).
|
||||||
|
|
||||||
|
To ensure the script runs correctly on other systems, either switch to Bash, or rewrite it in a POSIX compatible way.
|
||||||
|
|
||||||
|
This can generally done via `printf` as in the example. Be careful about strings with trailing linefeeds, as a `$(command substitution)` will strip them.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* StackOverflow: [Why does my bash code fail when I run it with sh?](https://stackoverflow.com/questions/15179446/why-does-my-bash-code-fail-when-i-run-it-with-sh)
|
Reference in New Issue
Block a user