[SOLVED] Arithmetic operation in bash script, multiply by decimal number, how?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Arithmetic operation in bash script, multiply by decimal number, how?
GNU bash, version 5.1.8(1)
How can i please do this in a bash script, i want to multiply certain number by decimal number 1.5
Non working attempts:
1)
Code:
result=$(( $number * 1.5 |bc -l ))
^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
^-^ SC2079: (( )) doesn't support decimals. Use bc or awk.
^-- SC2154: bc is referenced but not assigned (for output from commands, use "$(bc ...)" ).
^-- SC2154: l is referenced but not assigned.
Code:
2)
```
result=$(( $number * 1,5 ))
^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
GNU bash, version 5.1.8(1)
How can i please do this in a bash script, i want to multiply certain number by decimal number 1.5 Non working attempts:
Code:
result=$(( $number * 1.5 |bc -l ))
^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
^-^ SC2079: (( )) doesn't support decimals. Use bc or awk.
^-- SC2154: bc is referenced but not assigned (for output from commands, use "$(bc ...)" ).
^-- SC2154: l is referenced but not assigned.
Code:
2)
result=$(( $number * 1,5 ))
^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
output is decimal (5184.0) which i do not want, i want whole numbers only result
As asked many times in the past, do basic research first. From the bash documentation (bold/emphasis theirs):
Quote:
Originally Posted by Bash Documentation
Section "6.5 Shell Arithmetic":
The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by using the (( compound command, the let builtin, or the -i option to the declare builtin.
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error
So you cannot using bash alone. Doing basic research for other ways to script this yields a lot of suggestions and examples. The simplest being:
You did not declare the initial variable as an integer, and doing a simple divide by one gives you what you're after. Along with many other possibilities, all using standard Linux utilities:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.