Searene
Searene

Reputation: 27604

subtraction in linux shell if

In linux bash shell, how to subtract a variable from another in if? I've tried the following:

#!/bin/bash
start=0
end=1
if [ end - start -eq 1 ]; then
    echo "right"
fi

It doesn't work.

Upvotes: 0

Views: 4311

Answers (2)

Steve Vinoski
Steve Vinoski

Reputation: 20014

Since you specifically mentioned bash, use its arithmetic support:

#!/bin/bash
start=0
end=1
if ((end - start == 1)); then
    echo "right"
fi

Upvotes: 4

Nipun Talukdar
Nipun Talukdar

Reputation: 5387

Do something something like as shown below:

#!/bin/bash
start=0
end=1
if [ `expr $end - $start` -eq 1 ]; then
    echo "right"
fi

Upvotes: 1

Related Questions