As a general thing, having variables like N1
, N2
, etc... in bash and hoping to access them like N$i
with i
another variable is very bad. The cure for this lies in arrays. Here's how I would solve your problem using arrays instead:
n=()
i=0
while read -r -n1 n[i]; do
echo "Character $i is ${n[i]}"
((++i))
done < "$INPUT"
At this point, you have your characters in the array n
(well, not all of them). You can have access to character k+1
with
${n[k]}
(remember that array indexes start from 0).
Another possibility is to slurp everything in a string, it's as simple as this:
string=$( < "$INPUT" )
and you can have access to character k+1
with
${n:k:1}
(taking the substring of length 1 starting at index k
). Looping on all characters could be done as:
for ((k=0;k<${#string};++k)); do
echo "Character $k is '${string:k:1}'"
done