Ubuntu – What does “:” (colon) operator in a bash variable expansion: VAR=${TEMP:3}

bash

What is the meaning of the following line in a variable in bash?

VAR=${TEMP:3}

Best Answer

This is variable expansion and works like this (notice this is only bash and ksh specific and will not work in a POSIX shell):

$ x=1234567890
$ echo ${x:3}
4567890
$ echo ${x:7}
890
$ echo ${x:3:5}
45678

  • ${var:pos} means that the variable var is expanded, starting from offset pos.
  • ${var:pos:len} means that the variable var is expanded, starting from offset pos with length len.