From 1f83d59f0e5bb00f42d219fffa21d60692ccf6d9 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 1 Sep 2020 17:58:24 -0700 Subject: [PATCH] Created SC3028 (markdown) --- SC3028.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SC3028.md diff --git a/SC3028.md b/SC3028.md new file mode 100644 index 0000000..6310e11 --- /dev/null +++ b/SC3028.md @@ -0,0 +1,35 @@ +## In POSIX sh, VARIABLE is undefined. + +(or "In dash, ... is not supported." when using `dash`) + +### Problematic code: + +```sh +#!/bin/sh +echo "$HOSTNAME $UID $RANDOM" +``` + +### Correct code: + +Either switch to a shell like `bash` that supports the special variable you're trying to use, or use an external command to get the information you want: + +```sh +#!/bin/sh +echo "$(hostname) $(id -u) $(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null)" +``` + +### Rationale: + +The variable you are attempting to use is a special variable in bash or ksh. To get the same information from `dash` or POSIX `sh`, use an external command instead. + +### 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. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!