3 SC3057
Lucas Larson edited this page 2022-05-10 10:13:33 -04:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

In POSIX sh, string indexing is undefined.

(or "In dash, ... is not supported." when using dash)

Problematic code:

#!/bin/sh
echo "Your initial is ${USER:0:1}"

Correct code:

Either switch to a shell that does support string indexing via parameter expansion, like bash or ksh, or rewrite with cut:

#!/bin/sh
echo "Your initial is $(printf '%s' "$USER" | cut -c 1)"

To find the last argument passed to a shell script without using bashs ${@:$#}- or ${@: -1}-style string indexing, use the following, which even “works in the unix v7 bourne shell from 1979”:

#!/bin/sh
for argument in "$@"; do
  : # `:`, also called as `true`, is a no-op here
done
printf '%s\n' "${argument-}"

Rationale:

String indexing is a bash and ksh extension, and does not work in dash or POSIX sh.

Exceptions:

If you only intend to target shells that supports this feature, you can change the shebang to a shell that guarantees support, or ignore this warning.

You can use # shellcheck disable=SC3000-SC4000 to ignore all such compatibility warnings.

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!