

I will cover some of them with examples:ĪLSO READ: How to check if string contains numbers, letters, characters in bash Method 1: Bash split string into array using parenthesis Now your variable can have strings or integers or some special characters, so depending upon your requirement you can choose different methods to convert string into an array. To overcome this we convert and split string into array. When we execute the script, we see that number of elements in myvar is 1 even when we have three elements # /tmp/split-string.sh This we can verify by counting the number of elements in the myvar variable

Sample script with variable containing stringsįor example in this script I have a variable myvar with some strings as elementĮcho "Number of elements in the array: if I want to iterate over individual element of the myvar variable, it is not possible because this will considered as a variable and not an array. The main reason we split string into array is to iterate through the elements present in the array which is not possible in a variable. How to create array from string with spaces? Or In bash split string into array? We can have a variable with strings separated by some delimiter, so how to split string into array by delimiter? Some more examples to convert variable into array in bash.Method 4: Bash split string into array using tr.Method 3: Bash split string into array using delimiter.Method 2: Bash split string into array using read.Method 1: Bash split string into array using parenthesis.

