---
title: Parsing a CSV file using Bash
description: In this post we'll look at a simple bash script to parse through a .csv file and do something with the data
date: 2020-04-13
tags: Bash, csv
source: https://brianchildress.co/blog/parse-csv-file-in-bash
---

# Parsing a CSV file using Bash

Often times I need to do something multiple times with different input values. Like recently I needed to add a bunch of folks to a team in GitHub for an upcoming training session, easy to do for one or two, not for 200+. Luckily I had everyone's information in a .csv file, that turned into a quick bash script, easy peezy.

The command to loop through a .csv file is pretty simple, it looks like:

_parse-data.sh_
```sh
#!/bin/bash

FILE = $1

while IFS="," read f1 f2
do
  # Do something with that value, like a cURL command
  echo "First name is $f1"
  echo "Last name is $f2"
done < $FILE
```

Here I'm using the Internal Field Separator (IFS) to separate each line by the "," value. For each field I'm then representing it's value with the `f1 f2...` values. Note: each field in the _.csv_ should be represented with a variable, this will cause issues if your _.csv_ file contains more fields than are read into the _while_ loop.

Example data:

_data.csv_  
```csv
First_Name, Last_Name
James,Dean
Spider,Man
...
```

To use:

```
./parse-data.sh data.csv
```

Result:

```
First name is James
Last name is Dean
First name is Spider
Last name is Man
```
