Jaseem
Jaseem

Reputation: 2285

Integer addition in shell

Here is my simple shell code. I want the result to be 2.Shell treats everything as a string. How can i get this done ?

num=1
num=$(( $num + 1 ))

EDIT :

Complete code : Whats wrong in this if i want to print from 1 to 10 ?

#! /bin/bash

num=1
until test $num -eq 10
do
    num=$(( $num + 1 ))
    echo $num
done

Upvotes: 23

Views: 74252

Answers (8)

NPE
NPE

Reputation: 500357

In bash, you don't need to do anything special:

$ num=1
$ num=$(( $num + 1 ))
$ echo $num
2

Upvotes: 18

Hemant Metalia
Hemant Metalia

Reputation: 30648

try this

$ num=1; num=`expr $num + 1`; echo $num;

EDIT:

More efficient would be:

num=$(( num + 1 ))

Thanks @Charles Duffy for your comment.

Upvotes: 0

potong
potong

Reputation: 58420

This might work for you:

num=1; ((num++)); echo $num
2

or

num=1; echo $((++num))
2

for loops

for num in {1..10}; do echo $num; done

or (in bash at least)

for ((num=1; num<=10; num++)) { echo $num; }

second loop more useful when more programming involved:

for (( num=1,mun=10; num<=10; num++,mun--)) { echo $num $mun; }

Upvotes: 2

Juha Laiho
Juha Laiho

Reputation: 596

@tonio; please don't advocate using subshell (` ... or $( ... ) ) constructs when they're not needed (to keep confusion to the maximum, $(( ... )) is not a sub-shell construct). Sub-shells can make a tremendous performance hit even with rather trivial amounts of data. The same is true for every place where an external program is used to do somethign that could be done with a shel built-in.

Example:

    num=1
    time while [[ $num -lt 10000 ]]; do
            num=$(( num+1 ))
    done
    echo $num
    num=1
    time while /bin/test $num -lt 10000; do
            num=$( /bin/expr $num + 1 )
    done
    echo $num

Output (run in ksh on Linux):

real    0m0.04s
user    0m0.04s
sys     0m0.01s
10000

real    0m20.32s
user    0m2.23s
sys     0m2.92s
10000

...so run-time factor of 250, and CPU-time factor of 100. I admit the example I used was a exaggerated one, with explicitly requiring all built-ins to be bypassed, but I think the point was made: creating new processes is expenisve, avoid it when you can, and know your shell to recognise where new processes are created.

Upvotes: 3

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

works for me

$ num=1
$ num=$(( $num + 1 ))
$ echo $num
2

What output do you get?

Read more about bash Arithmetic @ tldp

EDIT

To do something 10 times in bash you can use (using brace-expansion}

$ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10

However, you cannot use variables between the {}. If this is the case, use seq instead.

Upvotes: -1

totaam
totaam

Reputation: 1311

You are not specifying which shell you are using, but the most concise form I know is this one (works at least in bash):

num=$[num+1]

If only incrementing by one and changing the variable itself rather than printing/assigning, then:

((num++))

Is a better/more elegant solution. See dogbane's answer for that.

If looping over the values, I would use this form instead:

for i in `seq 1 10`; do
   echo $i
done

Upvotes: 1

dogbane
dogbane

Reputation: 274612

Use ((num++)) as shorthand for incrementing num.

$ num=1
$ ((num++))
$ echo $num
2

Upvotes: 0

SiegeX
SiegeX

Reputation: 140327

You just did:

$ num=1; num=$(( $num + 1 ));echo $num
2

Note: You don't need to quote variables inside $(( )). Also, you can just use $((num++))

Upvotes: 1

Related Questions