Search code examples
linuxbashparameter-passingcommand-substitution

Passing multi-line command output to echo outputs only a single line


I have 2 files in my folder as shown below

$ ls -l
total 1
-rw-r--r-- 1 user user-group 0 May 10 14:49 test
-rw-r--r-- 1 user user-group 0 May 10 14:49 test1

Listing the files is showing normal. But why does below command shows both the lines merged in a single line in bash?

$ echo `ls -l`
total 1 -rw-r--r-- 1 user user-group 0 May 10 14:49 test -rw-r--r-- 1 user user-group 0 May 10 14:49 test1

Solution

  • Since you are using command substitution here and echo, inorder to get echo to show your output on multiple lines you will need to quote the output.

    $ echo "`ls -l`"