In one bash script i found the next construction:
if [[ "${xvar[id]:0:${#cnt}}" != "$cnt" ]]; then
Can someone explain what the above condition does?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The complicated expression is:
${xvar[id]:0:${#cnt}}.$xvarmust be an array, possibly associative. If it is associative, the part${xvar[id]}refers to the element of the array identified by the string ‘id’; if not, then it refers to the element indexed by variable$id(you’re allowed to omit the nested$), as noted by chepner in a comment.The
${xxx:0:${#cnt}}part of the expression refers to a substring from offset 0 to the length of the variable$cnt(so${#cnt}is the length of the string in the variable$cnt).All in all, the test checks whether the first characters of
${xvar[id]}are the same as the value of$cnt, so is the value in$cnta prefix of the value in${xvar[id]}.