Use find instead of ls to better handle non-alphanumeric filenames. Open
dirs=$(ls -l version | egrep '^d' | awk '{print $9}' | grep -v alpha | sort --version-sort -r)
- Read upRead up
- Exclude checks
Use find instead of ls to better handle non-alphanumeric filenames.
Problematic code:
ls -l | grep " $USER " | grep '\.txt$'
Correct code:
find . -maxdepth 1 -name '*.txt' -user "$USER"
Rationale:
ls
is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read.
Here's an example:
$ ls -l
total 0
-rw-r----- 1 me me 0 Feb 5 20:11 foo?bar
-rw-r----- 1 me me 0 Feb 5 2011 foo?bar
-rw-r----- 1 me me 0 Feb 5 20:11 foo?bar
It shows three seemingly identical filenames, and did you spot the time format change? How it formats and what it redacts can differ between locale settings, ls
version, and whether output is a tty.
ls
can usually be substituted for find
if it's the filenames you're after.
If trying to parse out any other fields, first see whether stat
(GNU, OS X, FreeBSD) or find -printf
(GNU) can give you the data you want directly.
Exceptions:
If the information is intended for the user and not for processing (ls -l ~/dir | nl; echo "Ok to delete these files?"
) you can ignore this error with a [[directive]].
Notice
Original content from the ShellCheck https://github.com/koalaman/shellcheck/wiki.
egrep is non-standard and deprecated. Use grep -E instead. Open
dirs=$(ls -l version | egrep '^d' | awk '{print $9}' | grep -v alpha | sort --version-sort -r)
- Read upRead up
- Exclude checks
egrep is non-standard and deprecated. Use grep -E instead.
Problematic code:
egrep 'foo|bar' file
Correct code:
grep -E 'foo|bar' file
Rationale:
egrep
is a non-standard command. Its functionality is provided in POSIX by grep -E
. POSIX grep says:
This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.
man grep for GNU says:
Direct invocation as either egrep or fgrep is deprecated
Exceptions:
ShellCheck will fail to recognize when functions override egrep
. Consider giving it a different name or [[ignore]] this error.
Notice
Original content from the ShellCheck https://github.com/koalaman/shellcheck/wiki.
TODO found Open
# TODO: remove this once bundler issue is fixed
- Exclude checks