What does ":" (colon) operator in a bash variable expansion: VAR=${TEMP:3}?

Solution 1:

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.

Solution 2:

in bash it cuts away the first 3 characters of a (string) variable:

$ VAR="hello world"
$ echo ${VAR:3}
lo world

have a look at 'substring extraction' here: http://www.tldp.org/LDP/abs/html/string-manipulation.html .

Solution 3:

This operator cuts off the first 3 characters of variable TEMP and assigns the rest to variable VAR.