Issue
This is not bash. This is sh. ${foo:0:1} doesn't work. Results in bad substitution.
foo="Hello"
How do i print each character individually? I am looking for an approach that requires no external commands.
Solution
Expanding on my comment: the traditional Bourne shell doesn't really have the sort of built-in facilities for string manipulation that are available in more modern shells. It was expected that you would rely on external program for these features.
For example, using something as simple as cut
, we could write:
foo="hello"
len=$(echo "$foo" | wc -c)
i=1
while [ "$i" -lt "${len}" ]; do
echo "$foo" | cut -c"$i"
i=$(( i + 1 ))
done
Which would output:
h
e
l
l
o
Commands like cut
are "standard" shell scripting commands, despite not being built into the shell itself.
Answered By - larsks Answer Checked By - Marie Seifert (WPSolving Admin)