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: 
Pure bash solution:

```
myStr='field1;field2;field3;field4;'
x="${myStr//[^;]}"                   # remove everything but the delimiter ';'
echo "${#x}"                         # get the length - outputs 4

```
```
#!/bin/bash
DATA="field1;field2;field3;field4;"
# save IFS
OLDIFS=$IFS
# set new IFS
IFS=";"
# set DATA as position parameters
set -- ${DATA//\*/\\*}
# simply get parameter count
echo "Fields: $#"
# restore IFS 
IFS=$OLDIFS

```