# Strings operations in bash scripting language: ## To extract a substring from a string: ``` a="This is a normal long string" b=${a:10:6} echo $b ``` => normal ## Similar outcome with substr: Syntax is: substr STRING POS LENGTH ``` expr substr "This is the same string" 13 4 ``` => same ## And yuet another method with cut -c_: ``` echo "This is the same string" 13 4 |cut -c13-16 cut -c13-16 <<<"This is the same string" ``` => same ## Length of a string: ``` a="This is a normal long string" echo Count of a is ${#a} ``` => Count of a is 28 #ASCII conversion functions: These 2 functions exist in other languages. Here is just an implementation that can be used in bash: ``` chr() { [ "$1" -lt 256 ] || return 1 printf "\\$(printf '%03o' "$1")" } ord() { LC_CTYPE=C printf '%d' "'$1" } ``` Usage: chr 65 A ord A 65 Other good resources for printing different strings: https://www.baeldung.com/linux/printf-echo https://www.baeldung.com/linux/shell-ascii-value-character ## Adding 2 hex numbers ``` printf "0x%X\n" $(( 0x1000 + 0xA000 )) ``` => 0xB000 ## Add a space after 2 characters: ``` echo "AnAxIsUp" | grep -o .. | xargs ``` => An Ax Is Up ## xxd tool to show a file,or part of it in hexadecimal ``` xxd -l 32 filename.txt ``` Will print first 32 bytes of filename.txt in hexa and acii ``` xxd -l 32 -p filename.txt ``` Will print first 32 bytes of filename.txt in hexa only