Shell

Shell

Made by DeepSource
Unicode quote detected SH-1110
Anti-pattern
Major

The highlighted line in the code contains a unicode quote. It is recommended to delete and retype it if a quote was intended.

Unused variable SH-2034
Anti-pattern
Major

Variables that are declared but not used for anything should be removed.

Outdated expr statement SH-2003
Anti-pattern
Minor

According to POSIX: > The expr utility has a rather difficult syntax [...] In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. Newly written scripts should avoid expr in favor of the new features within the shell.

Non-alphanumeric names may have been skipped SH-2038
Anti-pattern
Major

By default, xargs interprets spaces and quotes in an unsafe and unexpected way. Whenever it's used, it should be used with -0 or --null to split on \0 bytes, and find should be made to output \0 separated filenames. POSIX does not require find or xargs to support null terminators, so you can also use find -exec <command> +.

Consider escaping $ to make it a literal SH-1135
Anti-pattern
Major

The script appears to be closing a double quoted string for the sole purpose of making a dollar sign $ literal. This will work, but a better way is to escape it with a backslash. This allows the double quoted string to continue uninterrupted, thereby reducing the visual noise of stopping and starting quotes in the middle of a shell word.

Consider using braces to expand arrays SH-1087
Anti-pattern
Major

Some languages use the syntax $array[index] to access an index of an array, but a shell will interpret this as $array followed by the unrelated literal string (or glob) [index]. Here, curly braces are needed to tell the shell that the square brackets are part of the expansion.

Escaping a non-special character SH-1001
Anti-pattern
Minor

Trying to escape something that has no special meaning when escaped. The backslash will simply be ignored here. If the backslash was supposed to be literal, it is recommended to enclose it in a single quote or escape it. If you wanted it to expand to something, rewrite the expression to use printf (or in bash, $'\t').

Script uses carriage return SH-1017
Anti-pattern
Major

The script uses Windows/DOS style \r\n line terminators instead of UNIX style \n terminators. The additional \r aka ^M aka carriage return characters will be treated literally, and result in all sorts of strange bugs and messages. You can verify this with cat -v yourfile and see whether or not each line ends with a ^M.

Increase precision by replacing a/b*c with a*c/b SH-2017
Anti-pattern
Major

Consider using ${#variable} SH-2000
Anti-pattern
Minor

Consider using ${#variable} to get the number of characters in a variable. This is the same result as "$( echo "$variable" | wc -m )" when "$variable" only contains single-byte characters, it's also the same as "$( echo "$variable" | wc -c )"

Consider reformatting SH-1079
Anti-pattern
Major

This is a companion issue for SH-1078 which is triggered by unclosed strings. The quote here is interpreted as the closing quote for the previous string, but appears suspicious due to the character next to it.

Unnecessary use of multiple line terminators SH-1045
Anti-pattern
Major

Trailing spaces after \ SH-1101
Anti-pattern
Major

\\ is used before the line break to break a line. But, if there are spaces after the backslash, the escape will apply to the space instead of the line break, and the command will not continue on the next line.

Avoid use of ls | grep SH-2010
Anti-pattern
Major

Parsing ls is generally a bad idea because the output is fragile and human readable. To better handle non-alphanumeric filenames, use a glob. If you need more advanced matching than a glob can provide, use a for loop.

Unnecessary use of echo SH-2005
Anti-pattern
Major

Consider using cmd instead of echo $(cmd).

Glob used with grep SH-2063
Anti-pattern
Major

Unnecessary use of $ / ${} with arithmetic variable SH-2004
Anti-pattern
Critical

Use of $ or ${..} on regular variables in arithmetic context is unnecessary, and can even lead to subtle bugs. This is because the content of $((..)) is first expanded into a string, and then evaluated as an expression:

Can't escape ( or ) inside [[..]] SH-1029
Anti-pattern
Major

You don't have to -- and can't -- escape ( or ) inside a [[ .. ]] expression like you do in [ .. ]. It is recommended remove the escaping.

Detected a literal where tab, linefeed or cariage return might be expected SH-1012
Anti-pattern
Major

Detected a \t, \n or \r in a context where they just become regular letters t, n or r. Most likely, it was intended as a tab, linefeed or carriage return. To generate such characters (plus other less common ones including \a, \f and octal escapes) , use printf as in the example. The exception is for linefeeds that would be stripped by command substitution; in these cases, use a literal quoted linefeed instead.

[ .. ] is not a part of shell syntax SH-1014
Anti-pattern
Critical