Being new to shell scripting, I am not clear about the Quoting and splitting concepts in bash. In the below snippet:
array1=("france-country":"Italy-country":"singapore-country")
echo ${#array1[@]}
IFS=":-"
for i in ${array1[@]}
do
echo "$i"
done
unset IFS
with IFS being :-
, I thought the result would be:
france-country
Italy-country
belgium-country
as I had quoted them("france-country"). I think it should not get split on "-". But the results were:
france
country
Italy
country
belgium
country
It would be great if someone can point me out my mistake in understanding.
For your problem you can simply change the field separator as
:
i.e.IFS=:
because each country name is separated by:
not:-
in your exampleFYI, array elements in bash are separated by
space
so the whole string"france-country":"Italy-country":"singapore-country"
is a single element of the array thereforeecho ${#array1[@]}
will always be1
. So I do not see any use of an array in this example. Simple variable would have been suffice.