Can anyone tell me the meaning of ${p:2:1} in shell scripting as in:
var1=""
for p in [1,2,3,4]
do
var1="${var1} ${p:2:1}"
done
Can anyone tell me the meaning of ${p:2:1} in shell scripting as in:
var1=""
for p in [1,2,3,4]
do
var1="${var1} ${p:2:1}"
done
That is a parameter expansion (Bash manual), in particular of the form:
${parameter:offset:length}
which is described as "substring expansion". It extracts characters from the variable starting at offset (starting at zero) and going for length characters. In your case, ${p:2:1} extracts the third character.
Example:
$ p=abcd
$ echo "${p:2:1}"
c
That's a substring. It's taking from the second character (counting from 0) of the string p a substring of length 1.