Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
uxwspr60:/home/orcimp> cat test.sh
alpha='abcdefghijklmnopqrstuvwxyz'
print ${alpha:5:5}uxwspr60:/home/orcimp> test.sh
test.sh[2]: ${alpha:5:5}: The specified substitution is not valid for this command.

The bash syntax ${alpha:5:5} will not work on HP-UX. Following are 4 solutions that will provide the fghij result when $alpha is 10 characters or longer. But when $alpha is less than 10 characters, they produce different results!
For the sed solution below, if $alpha is less than 10, sed outputs the string as is with no change:
echo $alpha | sed 's/^.....\(.....\).*/\1/'
For expr, if $alpha is less than 10, output will be null:
expr $alpha : ".....\(.....\)"
For the ksh variable manipulations below, if $alpha is less than 10, output will be null:
alpha=abcdefghijklmnopqrstuvwxyz
beyond10=${alpha#??????????}
first10=${alpha%$beyond10}
last5=${first10#?????}
echo $last5For those same variable manipulations - but nested all together, if $alpha is less than 10, it will still output the shorter-than-5 substring.
echo ${alpha#?????%${alpha#??????????}}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |