View Single Post
  #8   (View Single Post)  
Old 6th September 2008
drl's Avatar
drl drl is offline
Port Guard
 
Join Date: May 2008
Posts: 19
Default

Hi.

Some shells have this built-in for variables:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate bash substring expansion.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo
echo " Results:"
t="0123456789abcdefghij"

echo
echo $t
echo

i=0
echo " First 5 ${t:$i:5}"
echo
i=-5
echo " Last  5 ${t:$i:5}"
echo
i=3
echo " 3 from 4 ${t:$i:3}"

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results:

0123456789abcdefghij

 First 5 01234

 Last  5 fghij

 3 from 4 345
cheers, drl
Reply With Quote