From d6bacbfe3545bc257d671d769c1daca83e45012c Mon Sep 17 00:00:00 2001 From: Lucas Larson <91468+LucasLarson@users.noreply.github.com> Date: Wed, 31 Mar 2021 14:07:54 -0400 Subject: [PATCH] =?UTF-8?q?create=20for=20documenting=20POSIX=20shell?= =?UTF-8?q?=E2=80=99s=20undefined=20`$((x=20**=20y))`=20for=20exponents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SC3019.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 SC3019.md diff --git a/SC3019.md b/SC3019.md new file mode 100644 index 0000000..9d05ef2 --- /dev/null +++ b/SC3019.md @@ -0,0 +1,22 @@ +## In POSIX sh, exponentials are undefined. + +### Problematic code: + +ShellCheck noticed you're using `**`-notation with two asterisks to obtain an exponent's value. Some examples: + +```sh +#!/bin/sh +echo $((2 ** 3)) # Using `**` for an exponent is undefined in POSIX sh. +``` + +### Correct code: + +Other possibilities exist: +```sh +#!/bin/sh +echo $((2 * 2 * 2)) # equivalent to `bash`'s `echo $((2 ** 3))` +echo '2 ^ 3' | bc # piping the formula to `bc` to parse the output https://stackoverflow.com/a/13111995 +printf '2 ^ 3\n' | bc # piping using `printf` (newline `\n` is mandatory) +``` + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX! \ No newline at end of file