I have a bash script that I want to output the variables into a .csv file. Most of the variable I can have the variable output on one line but I'm having a problme with variables that are multiline. The two variables are passed the process id of a process and the process listing contains new line characters. I thought if I stripped out the newline characters that you could assign it to the variable but it complains "No such file or directory in the variable substition". The other variable is just netstat -an | grep <port>. I don't know how to use arrays in bash but ideally i'd like to be able to keep the newlines in the csv file, if it's possible, since i'm sticking with comma as the separator. Any help would be really appreciated. Hitting my head off the wall!
VAR1="$(ps -ef | grep 27656 | tr '\n' ' ' | sed -e '/s */g')"
(printf '%s\n' $VAR1)