adding integers in BASH

 Addition in Bash is notoriously tricky and it almost never works as expected; it usually appends/joins two items as if they were strings regardless of whether they are in fact integers.

This example works.

Create a textfile containing lines of text, e.g. 

a

b

c

d

Call it "list.txt".

Save this into a bash script and run it:


#!/bin/bash

counter=0

for i in `cat list.txt` ; 

do

  echo $counter

  ((counter=counter+1))

done

Popular posts from this blog

Chunk validation failed

The most useful defaults-write options

Erasing firmware password