Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Looping over comma-separated values in Bash#
Given a file (or a process) that produces comma separated values, here’s how to split those into separate variables and use them in a bash script.
The trick is to set the Bash IFS to a delimiter, then use my_array=($my_string) to split on that delimiter.
Create a text file called data.txt containing this:
1first,12second,2You can create that by doing:
1echo 'first,12second,2' > /tmp/data.txtTo loop over that file and print each line:
1for line in $(cat /tmp/data.txt);2do3 echo $line4doneTo split each line into two separate variables in the loop, do this:
1for line in $(cat /tmp/data.txt);2do3 IFS=$','; split=($line); unset IFS;4 # $split is now a bash array5 echo "Column 1: ${split[0]}"6 echo "Column 2: ${split[1]}"7doneOutputs:
1Column 1: first2Column 2: 13Column 1: second4Column 2: 2Here’s a script I wrote using this technique for the TIL Use labels on Cloud Run services for a billing breakdown:
1#!/bin/bash2for line in $(3 gcloud run services list --platform=managed \4 --format="csv(SERVICE,REGION)" \5 --filter "NOT metadata.labels.service:*" \6 | tail -n +2)7do8 IFS=$','; service_and_region=($line); unset IFS;9 service=${service_and_region[0]}10 region=${service_and_region[1]}11 echo "service: $service region: $region"12 gcloud run services update $service \13 --region=$region --platform=managed \14 --update-labels service=$service15 echo16done