eolas/neuron/57f4d44e-6e5d-4bdf-a12c-b7dac10ca90d/Strings_in_Bash.md
2024-10-21 13:04:02 +01:00

399 B

tags
shell
data-types

Strings in bash

Return a substring by index

myString="hello"
substring=${myString:0:3}
# hel

This is often used when looping through each character in a string.

Loop through each character in a string

str="hallelujah"
stringLength=$(expr length str)

for (( i=0; i<=${stringLength}; i++ )); do
    echo "${str:$i:1}"
done