home

Quickies, by Andrea Olivato

text

Split string to array in Bash without Awk!

This shows how to split a string into an array in Bash without using Awk or other programming languages.

#!/bin/bash

# Your string
string="one,two,three,four,five,end";

OLD_IFS="$IFS";

# Character used for splitting
IFS=",";

bar=( $string );
IFS="$OLD_IFS";

# Cycle for every splitted part
for (( i=0; i<${#bar[@]}; i++ )); do
	
	# Some echo to understand what you got
	echo "["$i"] "${bar[$i]};
	
	# Put your stuff here
	
done;

3 years ago

May 15, 2009
Comments (View)
blog comments powered by Disqus