-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC2039
You have declared that your script works with /bin/sh
, but you are using features that have undefined behavior according to the POSIX specification.
It may currently work for you, but it can or will fail on other OS, the same OS with different configurations, from different contexts (like initramfs/chroot), or in different versions of the same OS, including future updates to your current system.
Either declare that your script requires a specific shell like #!/bin/bash
or #!/bin/dash
, or rewrite the script in a portable way.
For help with rewrites, the Ubuntu wiki has a list of portability issues that broke people's #!/bin/sh
scripts when Ubuntu switched from Bash to Dash. See also Bashism on wooledge's wiki. ShellCheck may not warn about all these issues.
bash, ksh:
a=$' \t\n'
POSIX:
a="$(printf '%b_' ' \t\n')"; a="${a%_}" # protect trailing \n
Want some good news? See http://austingroupbugs.net/view.php?id=249#c590.
Bash:
echo $"foo $(bar) baz"
POSIX:
. gettext.sh # GNU Gettext sh library
# ...
barout=$(bar)
eval_gettext 'foo $barout baz' # See GNU Gettext doc for more info.
Or you can change them to normal double quotes so you go without gettext
.
Bash:
for ((init; test; next)); do foo; done
POSIX:
: $((init))
while [ $((test)) -ne 0 ]; do foo; : $((next)); done
Bash:
((a=c+d))
((d)) && echo d is true.
POSIX:
: $((a=c+d)) # discard the output of the arith expn with `:` command
[ $((d)) -ne 0 ] && echo d is true. # manually check non-zero => true
It takes extra care over terminal columns to make select loop look like bash's, which generates a list with multiple items on one line, or like ls
.
It is, however, still possible to make a naive translation for select foo in bar baz; do eat; done
:
while
_i=0 _foo= foo=
for _name in bar baz; do echo "$((_i+=1))) $_name"; done
printf '$# '; read _foo
do
case _foo in 1) foo=bar;; 2) foo=baz;; *) continue;; esac
eat
done
Bash, ksh:
grep aaa <<< "$g"
POSIX:
printf '%s' "$g" | grep aaa # since we want to avoid `echo`
See https://unix.stackexchange.com/tags/echo/info.
Bash:
echo "${TERM/%-256*}"
POSIX:
echo "$TERM" | sed -e 's/-256.*$//g'
# Special case for this since we are matching the end:
echo "${TERM%-256*}"
Bash:
printf '%q ' "$@"
POSIX:
# TODO: Interpret it back to printf escapes for hard-to-copy chars like \t?
# See also: http://git.savannah.gnu.org/cgit/libtool.git/tree/gl/build-aux/funclib.sh?id=c60e054#n1029
reuse_quote()(
for i; do echo -n \'; echo -n "$i" | sed -e "s/'/'\\\\''/g"; echo -n "' "; done
)
reuse_quote "$@"
Depends on what your expected POSIX shell providers would use.