First I was using IFS and read, but then I found -- IFS can only support single character as separator and sometimes I need a string to be the separator. Then, I was thinking maybe use awk and eval can do it. So I have a try --
eisen@PVGN34701109A:/tmp> cat file1
hah a#$hehe#$hoho
eisen@PVGN34701109A:/tmp> cat t2.sh
#!/bin/bash
eval $(awk -F'#\\\$' '{for (i=1;i<=NF;i++) print "VAR"i"='\''"$i"'\''"}' file1)
echo "VAR1= ${VAR1}"
echo "VAR2= ${VAR2}"
echo "VAR3= ${VAR3}"
eisen@PVGN34701109A:/tmp> ./t2.sh
awk: warning: escape sequence `\$' treated as plain `$'
VAR1= hah a
VAR2= hehe
VAR3= hoho
At first, I think it's fine. But immediately I found the issue -- in file1, the first value should be "hah a" -- there's 3 space inside, But after awk and eval -- there's only one space left... Then I tested with awk and eval individually, I found it's due to the eval will "eat" some space--
eisen@PVGN34701109A:/tmp> eval "VAR1='hah a'";echo $VAR1
hah a
eisen@PVGN34701109A:/tmp> eval 'VAR1="hah a"';echo $VAR1
hah a
I can't find solution myself. Please kind help and if there's any other better way to split one line and assign to variables in BASH, please share your idea. Thanks in advance.