LINUX.ORG.RU

История изменений

Исправление Zmicier, (текущая версия) :

(info "(bash) Shell Parameter Expansion")

Исходная версия Zmicier, :

'${PARAMETER:OFFSET}'
'${PARAMETER:OFFSET:LENGTH}'
     This is referred to as Substring Expansion.  It expands to up to
     LENGTH characters of the value of PARAMETER starting at the
     character specified by OFFSET.  If PARAMETER is '@', an indexed
     array subscripted by '@' or '*', or an associative array name, the
     results differ as described below.  If LENGTH is omitted, it
     expands to the substring of the value of PARAMETER starting at the
     character specified by OFFSET and extending to the end of the
     value.  LENGTH and OFFSET are arithmetic expressions (*note Shell
     Arithmetic::).

     If OFFSET evaluates to a number less than zero, the value is used
     as an offset in characters from the end of the value of PARAMETER.
     If LENGTH evaluates to a number less than zero, it is interpreted
     as an offset in characters from the end of the value of PARAMETER
     rather than a number of characters, and the expansion is the
     characters between OFFSET and that result.  Note that a negative
     offset must be separated from the colon by at least one space to
     avoid being confused with the ':-' expansion.

     Here are some examples illustrating substring expansion on
     parameters and subscripted arrays:

     $ string=01234567890abcdefgh
     $ echo ${string:7}
     7890abcdefgh
     $ echo ${string:7:0}

     $ echo ${string:7:2}
     78
     $ echo ${string:7:-2}
     7890abcdef
     $ echo ${string: -7}
     bcdefgh
     $ echo ${string: -7:0}

     $ echo ${string: -7:2}
     bc
     $ echo ${string: -7:-2}
     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 parameters:

     $ 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}

     $ echo ${@:7:2}
     7 8
     $ echo ${@:7:-2}
     bash: -2: substring expression < 0
     $ echo ${@: -7:2}
     b c
     $ echo ${@:0}
     ./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
     $ echo ${@:0:2}
     ./bash 1
     $ echo ${@: -7:0}


     If PARAMETER is an indexed array name subscripted by '@' or '*',
     the result is the LENGTH members of the array beginning with
     '${PARAMETER[OFFSET]}'.  A negative OFFSET is taken relative to one
     greater than the maximum index of the specified array.  It is an
     expansion error if LENGTH evaluates to a number less than zero.

     These examples show how you can use substring expansion with
     indexed arrays:

     $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)
     $ echo ${array[@]:7}
     7 8 9 0 a b c d e f g h
     $ echo ${array[@]:7:2}
     7 8
     $ echo ${array[@]: -7:2}
     b c
     $ echo ${array[@]: -7:-2}
     bash: -2: substring expression < 0
     $ echo ${array[@]:0}
     0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
     $ echo ${array[@]:0:2}
     0 1
     $ echo ${array[@]: -7:0}


     Substring expansion applied to an associative array produces
     undefined results.

     Substring indexing is zero-based unless the positional parameters
     are used, in which case the indexing starts at 1 by default.  If
     OFFSET is 0, and the positional parameters are used, '$@' is
     prefixed to the list.