Skip to content
Tomek Grzechowski edited this page Nov 16, 2017 · 16 revisions

Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.

Problematic code:

ls /directory | grep target_file_pattern

Correct code:

ls /directory/target_file_pattern

Rationale:

Matching non-alphanumeric characters with grep may require escaping. Typically it is cleaner to use the built in pattern matching or another command like find

Exceptions:

ls /directory | grep -v unwanted_target_file_pattern

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally