Chapter 3: Basic Shell Features

25

bcdef

$ set -- 01234567890abcdefgh

$ echo ${1:7}

7890abcdefgh

$ echo ${1:7:0}

$ echo ${1:7:2}

78

$ echo ${1:7:-2}

7890abcdef

$ echo ${1: -7} bcdefgh

$ echo ${1: -7:0}

$ echo ${1: -7:2} bc

$ echo ${1: -7:-2} bcdef

$ array[0]=01234567890abcdefgh

$ echo ${array[0]:7}

7890abcdefgh

$ echo ${array[0]:7:0}

$ echo ${array[0]:7:2}

78

$ echo ${array[0]:7:-2}

7890abcdef

$ echo ${array[0]: -7} bcdefgh

$ echo ${array[0]: -7:0}

$ echo ${array[0]: -7:2} bc

$ echo ${array[0]: -7:-2} bcdef

If parameter is ‘@’, the result is length positional parameters beginning at offset.

A negative offset is taken relative to one greater than the greatest positional parameter, so an offset of -1 evaluates to the last positional parameter. It is an expansion error if length evaluates to a number less than zero.

The following examples illustrate substring expansion using positional param- eters:

$ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h

$ echo ${@:7}

7 8 9 0 a b c d e f g h

$ echo ${@:7:0}