tamalsaha / learn-bash

0 stars 0 forks source link

IFS : How do I split a string on a delimiter in Bash? #10

Open tamalsaha opened 4 years ago

tamalsaha commented 4 years ago
tamalsaha commented 4 years ago
while IFS= read -r line
do
  echo "$line"
done < input_file

The internal field separator (IFS) is set to the null string to preserve leading and trailing whitespace which is the default behavior of the read command.

tamalsaha commented 4 years ago
str="Learn to Split a String in Bash Scripting"

IFS=' ' # space is set as delimiter
read -ra ADDR <<< "$str" # str is read into an array as tokens separated by IFS
for i in "${ADDR[@]}"; do # access each element of array
    echo "$i"
done

https://www.tutorialkart.com/bash-shell-scripting/bash-split-string/