What is the meaning of the following line in a variable in bash
?
VAR=${TEMP:3}
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
.zsh
=) I made a diet script in zsh
and lost 10kg in 1 week.
zsh
for sure makes you dumber at typing paths when you're not using it :)
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 .
This operator cuts off the first 3 characters of variable TEMP
and assigns the rest to variable VAR
.
:
has also other meanings, it's a bash builtin command, for example. I wanted to differ from that, because the question is obviously not about that. What the user means is the:
operator inside a variable name. I'm sorry, I don't want to annoy somebody, but your rollback makes no sense and should be reversed, but I don't want an edit-war here. Furthermore it's bash and scripting specific so why remove those tags?TEMP
andVAR
variables and changed the question the way that other answers lost the meaning.VAR
variable. But, however, you're free to change that one thing back. I wanted to make the question more general, less specific. It was never meant to sabotage other answers.