'Bash script - How to preserve new line character passed to bash function?

I have two files:

touch a.txt
touch b.txt

In bash I would like to list them each one in its own line.

#!/bin/bash
var=$(ls -1 *.txt)
echo "$var"

Because there are double quotes in: echo "$var" command each of file is displayed in separate lines (that is what I want):

a.txt
b.txt

Now to the problem. If I add function:

#!/bin/bash
function fun_cmd () {
     $1
}
var=$(ls -1 *.txt)
fun_cmd "echo $var"

and echo command displays them in one single line:

a.txt b.txt

In the last sample (function), I would like output to be displayed in two lines, just like first sample.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source