Of course.
When executed, the output (echo in line 9) is:
1
2
3
....until the length of the first line of the file "leerarchivo.sh"
but..the 'echo' in line 12 prints 0.
This is the result.
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
0
I would like the last '0' were 12! I don't understand why..
the while loop is starting in a sub shell and all the processing (until done) is processing in the same shell. when the loops finishes, the execution returns to the parent shell and the modified value of contador left in the older (subshell).
what you are getting (zero) is the value of the variable in the parent shell.
you can force the command to execute in the same shell with (...).
try:
contador=0
while read linea
do
echo ""
echo "$linea" | (while IFS="" read -n 1 caracter
do
contador=$((${contador}+1))
echo $contador
done
echo $linea
echo $contador )
done < leerfichero.sh