Question: Given the following data: 

```
 field1;field2;field3;field4;

```

How to get the number of fields in this string? 
For example, the command should give back **4** for this case.

Actually, I don't have the same number of contents in each line, but I need to return them all anyway. Once I manage to count the number of fields, I will also manage to make a loop to get them all.

Answer: 
You can say:

```
$ echo "field1;field2;field3;field4;" | grep -o ";" | wc -l
4

```

Alternatively,

```
$ tr -dc ';' <<< "field1;field2;field3;field4;" | wc -c
4

```

EDIT: In order to loop over the fields, you can say:

```
$ IFS=';' read -ra field <<< "field1;field2;field3;field4;"
$ for i in "${field[@]}"; do echo $i; done
field1
field2
field3
field4

```
The option I have provided still holds up without bugs or manipulation.

```
j=0
i=1

while [ "$j" ]
do 
    j="$(cat dataFile.txt | cut -d[delimiter] -f$i)" 
    i="$(($i+1))"
done 

echo "$(($i-2))"

```

I initialize a variable `$j` to hold the results, and as long as the results exist, the loop counts each run. `cut` always runs till 2 past the field, explaining my `$(($i-2))` statement.

Put your data-file's delimiter after cut -d
I used a comma to separate my fields, so my actual code has:
cut -d, -f$i

I hope this makes sense.