Shell

Shell

Made by DeepSource

Consider using ${variable//search/replace} SH-2001

Performance
Major

Consider using parameter expansion to search and replace data.

In the problematic code the value is passed to sed to do this, but for simple substitutions, parameter expansion can do it with less overhead.

Problematic code:

string="stirng" ; echo "$string" | sed -e "s/ir/ri/"

Preferred code:

string="stirng" ; echo "${string//ir/ri}"

Exception:

Occasionally a more complex sed substitution is required. For example, getting the last character of a string.

string="stirng" ; echo "$string" | sed -e "s/^.*\(.\)$/\1/"

Utilizing some of the more complex capabilities of sed is required occasionally and it is safe to ignore this issue.