mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2011 (markdown)
23
SC2011.md
Normal file
23
SC2011.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
## Use `find -print0` or `find -exec` to better handle non-alphanumeric filenames.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ls | xargs wc -w
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
find . -maxdepth 1 -print0 | xargs -0 wc -w
|
||||||
|
```
|
||||||
|
|
||||||
|
```sh
|
||||||
|
find . -maxdepth 1 -print0 -exec wc -w
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Using `-print0` separates each output with a NUL character, rather than a newline, which is safer to pipe into `xargs`. Alternatively using `-exec` avoids the problem of piping and parsing filenames in the first place.
|
||||||
|
|
||||||
|
See [[SC2012]] for more details on this issue.
|
Reference in New Issue
Block a user