0
prefix="N774"
prefix_trunc=${prefix:1:3}

what does ${prefix:1:3} do. I have searched for it but i did not get any answere.

Thank you

  • P.E. parameter expansion see gnu.org/software/bash/manual/html_node/… – Jetchisel Apr 9 at 7:06
  • If you have bash installed. PAGER='less +/^[[:space:]]*parameter\ expansion' man bash – Jetchisel Apr 9 at 7:07
  • echo "${prefix_trunc}" should show you what it does, change the numbers and see what change and read the manual. – Jetchisel Apr 9 at 7:08
0

What does ${prefix:1:3} do?

It is called Shell Parameter Expansion.

Shell Parameter Expansion

The $ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded can be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

When braces are used, the matching ending brace is the first } not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

The basic form of parameter expansion is:

${parameter}

The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.

...

${parameter:offset}
${parameter:offset:length}

Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions. This is referred to as Substring Expansion.

  • length must evaluate to a number greater than or equal to zero.
  • If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter.
  • If parameter is @, the result is length positional parameters beginning at offset.
  • If parameter is an array name indexed by @ or *, the result is the length members of the array beginning with ${parameter[offset]}.
  • Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1.

Source How-To: shell expansion - Linux - SS64.com

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.