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:
FILE = $1
while IFS="," read f1 f2
# 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.
First name is James