How to add arithmetic variables in a script
Your arithmetic evaluation syntax is wrong. Use any of the following (the first is extremely portable but slow, the second is POSIX and portable except to the Bourne shell and earlier versions of the Almquist shell, the last three require ksh
, bash
or zsh
):
a=`expr "$a" + "$num"`
a=$(($a+$num))
((a=a+num))
let a=a+num
((a+=num))
Or you can just skip the entire for
loop and just do:
wc -l folder/*
Or, if you only want the total:
cat folder/* | wc -l
Or with zsh and its mult_ios
option:
wc -l < folder/*
you can also use this code
a=`expr $a + $num`
echo $a
and MAKE SURE THAT THERE IS A SPACE ON BOTH SIDES OF + IN "$a + $num"
You could declare the type of variable first:
declare -i a=0
declare -i num=0