Bash arithmetic issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash arithmetic issue
# 1  
Old 07-22-2016
Bash arithmetic issue

I found the following issue by simply increasing a variable. The
Code:
((A++))

expression returns an error, the other expressions both return 0. Does anyone know why?

Code:
script.sh:

    #! /bin/bash

    A=0
    B=0
    C=0

    ((A++))   ; echo "${?}"
    ((B=B+1)) ; echo "${?}"
    ((C+=1))  ; echo "${?}"

# ./script.sh
1
0
0

# 2  
Old 07-22-2016
This is specified and expected behaviour. man bash:
Quote:
If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1.
A++ is post-increment, i.e. A is evaluated (to zero, thus return status 1) and then incremented. ++A will return the expected 0 .
These 3 Users Gave Thanks to RudiC For This Post:
# 3  
Old 07-22-2016
You are right - thank you very much! I couldn't find the info in my bash man page yet. Nevertheless, this is a stupid behaviour of the bash.
# 4  
Old 07-22-2016
Why stupid? It't explainable and logic.
# 5  
Old 07-22-2016
Hm, I found the point in the man page now. It was imho a bad decision to change the behaviour of the (()) expression. Why should a successful expression return a non zero exit code?

And still it is an inconsitent behaviour:

Code:
((A=0))

returns 1
Code:
 declare -i B ; B=B+0

returns 0
# 6  
Old 07-22-2016
You are aware what $? means, yes? It's a return code. It is not a mathematical result, but it is the result of the expression.

The result of the expression (( X = 0 )) is zero, which considered as a boolean means false, which as a return code means failure, so $? becomes 1.

The result of (( X = 1 )) is nonzero, which considered as a boolean means true, which as a return code means success, so $? becomes 0.

It does this because this is how mathematical functions work in most languages, which is what (( )) is for. If you don't want to do that, there's the old fashioned way.

Which is not what you showed, anyway:

Code:
$ B=B+0
$ echo B
B+0

$

Shell variables do not work that way.
# 7  
Old 07-22-2016
I don't want to struggle, but IMO there are at least two ways to make the same arithmetic expression:

Code:
((B=0))

returning 1

and

Code:
declare -i B ; B=B+0

returning 0

and I only wanted to point out that this is an inconsistent behaviour. But, ok, we have two different operators here: (()) and the 'declare' one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. UNIX for Beginners Questions & Answers

Compare bash arrays issue

Hello everyone, I need help comparing 2 arrays. the first array is static; the second array is not .. array1=( "macOS Mojave" "iTunes" ) cd /Volumes array2=( * ) # output of array2 macOS Mojave iTunes Mac me The problem occurs when I compare the arrays with the following code - ... (6 Replies)
Discussion started by: trexthurman
6 Replies

3. Shell Programming and Scripting

Arithmetic on a Float in bash

I am using bash I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows: number=85.152 A=${number%.*} #Converts float to integer typeset -i B=$(((A-20)/10)) #subtracting 20 is... (12 Replies)
Discussion started by: prodigious8
12 Replies

4. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

6. Shell Programming and Scripting

bash ls command file issue

ls -l /md01/EL/MarketData/inbound/ststr/INVENTORY* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'If I run the above from the command line the output to md_email is formatted correctly as 78213497 May 1 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120430.PINESTREET.CSV.done 77904740 May 2... (3 Replies)
Discussion started by: smenago
3 Replies

7. Shell Programming and Scripting

bash in perl issue

I use the following shell script in bash and it works fine. It returns 1 # cat /etc/httpd/conf/res.txt maldet(24444): {scan} scan completed on eicar_com.zip: files 1, malware hits 1, cleaned hits 0 # if ]; then echo 0 > /etc/httpd/conf/malflag; else echo 1 > /etc/httpd/conf/malflag;... (2 Replies)
Discussion started by: anilcliff
2 Replies

8. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

9. Shell Programming and Scripting

Bash Calculator issue

Hello, I'm relatively new to using bc so I could use some help. In this script im working on I want to have the bc function to calculate float numbers for imagemagicks convert charcoal. Below is what I'm talking about. There are no syntax errors but when it outputs the users frames for example 0-10... (2 Replies)
Discussion started by: jsells20
2 Replies

10. Shell Programming and Scripting

Need help is manipulating a file with some arithmetic operations using bash script

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 Now I need to manipulate the file in such a way that to all the numbers less than 163, 63 gets added and to all numbers greater than 163, 63 gets deducted.(The numbers... (2 Replies)
Discussion started by: shrijith1
2 Replies
Login or Register to Ask a Question