Shell

Shell

Made by DeepSource

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.

Please consider rewriting this using $((..)), ${} or [[ ]].

Problematic code:

i=$(expr 1 + 2)
l=$(expr length "$var")

Preferred code:

i=$((1+2))
l=${#var}

Exceptions:

sh doesn't have a great replacement for the : operator (regex match). The checker tries not to warn when using expr with :, but some inputs like op=:; expr string "$op" regex will still trigger it.

Other than that, all uses of expr can be rewritten to use modern shell features instead.

Bash has [[ string =~ regex ]], so expr .. : .. is not necessary there.