Hi.
You can use the special parameter expansion syntax:
Code:
#!/usr/bin/env sh
# @(#) s1 Demonstrate sub-string extraction of parameter expansion.
set -o nounset
echo
## Define function to list versions.
version()
{
for command
do
if command -v $command >/dev/null 2>&1
then
command -p $command --version | head -1
else
echo " (Warning -- command \"$command\" not found in PATH.)" >&2
fi
done
return 0
}
## List the version of the commands in this demonstration.
version bash head
echo
s="xyz"
l=${#s}
echo " There are $l characters in |$s|"
echo
for ((i=0;i<l;i++))
do
echo " Character $i of the string is |${s:$i:1}|"
done
exit 0
Producing:
Code:
% ./s1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
head (coreutils) 5.2.1
There are 3 characters in |xyz|
Character 0 of the string is |x|
Character 1 of the string is |y|
Character 2 of the string is |z|
See
man bash for details ... cheers, drl