I would just use cat -n file
But if you really want to use a bash loop:
i=0
while read; do
printf '%d %s\n' $(( ++i )) "$REPLY"
done < file
Update: I now prefer nl
to cat -n
, as the former is standard. To get the same result as cat -n
, use nl -b a "$file"
.