iTranslated by AI
[Bash] Converting between Strings, Byte Sequences, and Unicode Escape Sequences
Bash supports byte sequences and Unicode escape sequences.
echo -e "\xe3\x81\x82"
echo -e \\xe3\\x81\\x82"
あ
echo -e "\u3042"
echo -e \\u3042
あ
To generate characters from code points, use printf to generate Unicode escape sequences.
printf $(printf '\\U%08x' 0x3042)\\n
あ
To find the code point from a character, prepend a single quote to the character.
printf U+%X\\n \'あ
U+3042
In the POSIX specification, characters starting with a single or double quote are treated as numeric values [1].

1 http://mywiki.wooledge.org/BashFAQ/071
To generate a byte sequence escape sequence from a character, use hexdump.
echo -n あ | hexdump -e '"\\\x" 1/1 "%02x"'; echo;
\xe3\x81\x82
For a hexadecimal sequence that is not an escape sequence, xxd can be used.
echo -n あ | xxd -p
e38182
echo -n e38182 | xxd -r -p; echo
あ
To extract characters one by one from a string, use a for loop and index notation.
str=あいうえお; for ((i=0; i<${#str}; i++)); do echo ${str:$i:1}; done
あ
い
う
え
お
For for-in loops, combine it with sed.
for c in $(sed 's/./&\n/g' <(printf あいうえお)); do echo $c; done
あ
い
う
え
お
One-liners using sed, gawk, or grep are also available.
echo -n あいうえお | sed -e 's/\(.\)/\1\n/g'
echo -n あいうえお | gawk '{ gsub(/./, "&\n"); print }'
echo -n あいうえお | grep -Eo '.{1}'
At the time of this research, no command-line tools that support extended grapheme clusters were found. Developing such a tool would require a regular expression library that supports extended grapheme clusters or ICU.
[Update: July 2024]
GNU grep supports PCRE 2. For more details, refer to this article.
echo -e "ハ\u309Aハ\u309A"
パパ
echo -e "ハ\u309Aハ\u309A" | grep -oP '\X'
パ
パ
Discussion