How do you process a CSV file line by line into fields?
IntermediateConceptArrays, conditionals and loops
Answer
while IFS=, read -r username group shell _rest; do
[[ -z $username || $username == \#* ]] && continue # skip blanks and comments
echo "user=$username group=$group shell=${shell:-/bin/bash}"
done < <(tail -n +2 users.csv) # skip the header
The last variable receives the remainder of the line, so add _rest to absorb extra columns. This only handles simple CSV: quoted fields with embedded commas need a real parser (python3 -c 'import csv...', mlr, or csvkit).
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate