Skip to content
Daniel YC Lin edited this page Jan 4, 2018 · 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:

Matching the negative list.

ls /directory | grep -v NOT_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