expansion

Bash Substring

Extracts a zero-based substring; length may be omitted. Space separates a negative offset from the default operator. Use trusted arithmetic expressions.

Formal Syntax & Structure

bash syntaxbash
${p:offset:length}

Return Value

shell expansion result

Extracts a zero-based substring; length may be omitted. Space separates a negative offset from the default operator. Use trusted arithmetic expressions.

Working Example

Example Snippetbash
p=helloprintf '%s\n' "${p:1:2}" "${p: -2}"
Bash Reference