mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
create for documenting POSIX shell’s undefined $((x ** y))
for exponents
22
SC3019.md
Normal file
22
SC3019.md
Normal file
@@ -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!
|
Reference in New Issue
Block a user