You are not logged in.

#1 2022-12-03 17:34:15

SupKurtJ
Member
Registered: 2015-07-18
Posts: 46

[SOLVED] Bash scripting decimal multiplication

Hi,

I have an error with this syntax in bash :

#!/usr/bin/env bash
echo $((0.03*100))

It work in the terminal, but not on the script.

Do you have an idea please ?

Thanks

Last edited by SupKurtJ (2022-12-05 14:23:57)

Offline

#2 2022-12-03 17:47:14

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: [SOLVED] Bash scripting decimal multiplication

It work in the terminal

echo $SHELL

Offline

#3 2022-12-03 17:56:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] Bash scripting decimal multiplication

Bash (along with most of the common *nix shells) do not handle floating point arithmetic themselves.  You can use bc/cd for this - or depending on the need and source of the data, you can just change the effective units (e.g., many thermal sensors give values in thousandths of a degree - there is no decimal, so 18 degrees would be 18000; the last three digits can be trimmed or a demicimal inserted for display when printed).

Last edited by Trilby (2022-12-03 17:57:21)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2022-12-03 18:00:28

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: [SOLVED] Bash scripting decimal multiplication

Trilby wrote:

Bash (along with most of the common inferior *nix shells) do not handle floating point arithmetic themselves.

ftfy, cause zsh does tongue
Hence the inquiry on his interactive shell…

Offline

#5 2022-12-03 23:03:16

SupKurtJ
Member
Registered: 2015-07-18
Posts: 46

Re: [SOLVED] Bash scripting decimal multiplication

Indeed my terminal uses the zsh shell.
The script use bash.

I didn't know bash was this limitation concerning floating point arithmetic.

So either I use zsh on my script, or I use the bc package.

Thanks for your answers.

Last edited by SupKurtJ (2022-12-03 23:03:41)

Offline

Board footer

Powered by FluxBB