I have two variables var1 and var2 that could either be multiple lines or could be completely empty. For example, either one could look like:
line/1
line/2
or
I want to concatenate them so that when put together var1 outputs as is, and var 2 outputs as is right below the last line of var1. There are 4 cases, either var1 is empty and var2 is not, var 2 is empty and var 1 is not, both are empty, or neither are empty. I dont want any whitespace or an empty line if one of the variables is empty. So if var 1 is empty I do not want,
line/1
or vice versa.
Other than using a if, elif, else block, is there a way I could do this or do I HAVE to use an if else block.
In addition, for the last case where neither are empty how can I concatenate these two? I have tried
var3="${var1}\n${var2}"
but that doesn't seem to work. Any tips would be much appreciated.
\ndoes not interpolate a newline character, but a literal` followed by an. You can verify this by doing a, say,xxd <<<$var3`.