-2

I don't want output in one line, I want output just like a normal command.

ls -l
total 8

-rw-r--r-- 1 shubhamtatvamasi staff 0B Sep 3 13:33 file1

-rw-r--r-- 1 shubhamtatvamasi staff 0B Sep 3 13:33 file2

-rw-r--r-- 1 shubhamtatvamasi staff 0B Sep 3 13:33 file3

-rw-r--r-- 1 shubhamtatvamasi staff 0B Sep 3 13:33 file4

-rwxr-xr-x 1 shubhamtatvamasi staff 37B Sep 3 13:34 test.sh

script

cat test.sh
#!/bin/bash

LS=$(ls -l)

echo ${LS}
./test.sh

Out is comming in one line

total 8 -rw-r--r-- 1 shubhamtatvamasi staff 0 Sep 3 13:33 file1 -rw-r--r-- 1 shubhamtatvamasi staff 0 Sep 3 13:33 file2 -rw-r--r-- 1 shubhamtatvamasi staff 0 Sep 3 13:33 file3 -rw-r--r-- 1 shubhamtatvamasi staff 0 Sep 3 13:33 file4 -rwxr-xr-x 1 shubhamtatvamasi staff 37 Sep 3 13:34 test.sh
  • Use `echo "${LS}"` with quotes, some whitespaces are removed when you don't. – Aaron Sep 03 '19 at 08:39
  • 2
    Possible duplicate of [How to preserve spaces when outputting a shell variable?](https://stackoverflow.com/questions/22378755/how-to-preserve-spaces-when-outputting-a-shell-variable) (or [this one](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) it's a duplicate of) – Aaron Sep 03 '19 at 08:43

1 Answers1

1

You have to use double quotes with the variable if you want echo to print the value as is. Otherwise echo will print the output in a single line. Using double quotes will also enable you to use escape sequences within the output to format it further.

Lohit Gupta
  • 1,045
  • 6
  • 11