Bash – skipping a variable using while read in bash

bashscriptingshell

i'm reading a few variables from a file using

while read a b c; do
  (something)
done < filename

is there an elegant way to skip a variable (read in an empty value), i.e. if I want a=1 b= c=3, what should I write in the file?

Right now i'm putting

1 "" 3

and then use

b=$(echo $b | tr -d \" )

but this is pretty cumbersome, IMHO

any ideas?

Best Answer

With a blank field:

1 3

You can do:

if [[ -z c ]]
then
    c=b
    b=
fi

If your data is comma delimited, you can also do:

while IFS=, read a b c

(but you'll have problems if your fields contain commas, as you may in your current version if they contain quotes):

1,,3

Also, in your version instead of using tr, you can do:

b=${b//\"\"}

but you can eliminate that step altogether using the following with your current data format:

while IFS='"' read a b c

however, comma-delimited fields is a more common format.