#!/bin/sh fn="sample.txt" countLines() { local line counter=0 while read line; do counter=$((counter + 1)) done echo $counter return 0 } counter=`cat $fn | countLines` echo $counter