Friday 15 August 2014

bash - Splitting a comma separated string into multiple words so that I can loop through each word -


I want to split the string to use with for-loop in Bash. For example, I have this string

  Hello, mine, name, is, Mania   

and I want to divide it in different words So that I can loop through every word, can someone help me?

Using a word segmentation in the array is a very easy way: <<> S = "hello, mine, name, is, brain"

You set the input field separator to:

  IFS =, < / Code>  

and then you split the string into an array:

  a = ($ s)   

Result:

  for word in "$ {a [@]}"; Do echo "- [$ word]"; Done    

No comments:

Post a Comment