AvocadoRivalry
AvocadoRivalry

Reputation: 411

Comment out line in multiple line string in bash script

I'm trying to write a script that exports an array to loop through in the following way:

export fields="
a,1
b,2
c,3
...
"

for i in $fields
do
  IFS=","
  set $i
  ...
done

Is there a way to only comment out a single line in the list of field "tuples" that I'm using? In other words, if I want to run this and skip "b,2" is there a way to comment this line out without deleting the line?

Upvotes: 1

Views: 606

Answers (1)

chepner
chepner

Reputation: 531175

First, define a array that has one line per element (no need to export it):

fields=(
  # a,1
  b,2
  c,3
)

Note you can intersperse comment lines with the rest of the elements.

Then, iterate over the contents of the array and use the read command to split each element into two fields:

for line in "${fields[@]}"; do
    IFS=, read f1 f2 <<< "$line"
    ...
done

Upvotes: 1

Related Questions