How do I get the 2nd and 4th character from a string How do I get the 2nd and 4th character from a string bash bash

How do I get the 2nd and 4th character from a string


with bash variables, you can do this:

DOMAINNAME=abcdefCHAR2=${DOMAINNAME:1:1}CHAR4=${DOMAINNAME:3:1}echo "char2=$CHAR2, char4=$CHAR4"

gives:

char2=b, char4=d

explanation

the meaning of this: ${DOMAINNAME:3:1}means: take substring starting from the character at pos 3 (0-based, so the 4th character), and length = 1 character.