Read digit by digit in variable
I have been looking all over the web to a simple solution for this , but none of them solved my issue , specially related to a variable and not a file .
The objective is to count the number of digits in a specific variable , and then display one by one . A quick start - first i need to count the number of digits in variable - second i must set a loop with the number of digits and then start displaying them Code:
#!/bin/bash |
You don't really want to 'read', you want to get a substring from a string. Something like this:
Code:
Var='258J' |
Quote:
Quote:
If that is correct then you don't need the count at all and can simply use the shell's built in substitution operators to iterate the number, among other ways. Perhaps something like this (I leave the loop test as an exercise)... Code:
var=54321X |
Try a 'here string' for the read: https://unix.stackexchange.com/quest...-variable-bash
Try: echo $var | fold -s1 For #digits, Append: | wc -l |
You might like this ...
Code:
fold -w1 <<<"28173" Code:
grep -o . <<<"28173" . |
Untested:
Code:
printf '%s' "$var" | sed $'s/./&\n/g' | while read Chr do; |
Thanks all , NevemTeve example worked great .
|
All times are GMT -5. The time now is 04:04 AM. |