Assigning two variables with "#" using read in bash

How can I assign a string to a variable if it contains “#”.

Script:

while read var1 var2; do
    echo [$var1] [$var2]
done < "file.txt"

file.txt:

#var1
# var1

Result:

[#] [var1]

I am expecting it would be:

[#var1] []
[#] [var1]

The first data is being neglected and I don’t know why. How I can retrieve data with # in its prefix?


Source: shell

Leave a Reply