Two Variables = Two Cells
If the variables never contain special symbols like "
or ,
or linebreaks then it is as simple as
echo "$Var1,$Var2" > file.csv
If there could be a special symbol inside them, you have to quote them, for instance using
printf '"%s","%s"
' "${Var1//"/""}" "${Var2//"/""}" > file.csv
Two Variables = Two Columns With Multiple Cells (One Cell Per Line)
Without quoting
paste -d, <(echo "$Var1") <(echo "$Var2") > file.csv
With quoting
varToCol() {
sed 's/"/""/g;s/.*/"&"/' <<< "${!1}"
}
paste -d, <(varToCol Var1) <(varToCol Var2) > file.csv
For a pure bash solution replace the function with the following. Please note: Below bash solution adds an unnecessary empty field at the bottom of the column, if the variable ends with a newline character.
varToCol() {
local col="${!1//"/""}"
printf %s\n ""${col//$'
'/$'"
"'}""
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…