1

I am running WSL (Windows Subsystem for Linux) and when I use the ls -l function on the terminal, I get new lines printed i.e. each file is on a new line e.g.:

total 12K
drwxrwxrwx 1 user user 4.0K Apr  4 23:28 .
drwxr-xr-x 1 user user 4.0K Apr  4 23:28 ..
-rw-rw-rw- 1 user user    0 Mar 28 01:03 a1.file
-rw-rw-rw- 1 user user 4.5K Mar 23 16:27 books.xml
-rw-rw-rw- 1 user user    0 Mar 13 00:19 file1
-rw-rw-rw- 1 user user  513 Mar 13 00:19 file2.txt
-rw-rw-rw- 1 user user 2.8K Mar 13 12:42 file3.txt
-rw-rw-rw- 1 user user  405 Apr  4 23:26 ls_l.txt
-rw-rw-rw- 1 user user    0 Apr  4 23:28 ls_l2.txt
-rw-rw-rw- 1 user user   89 Mar 13 00:18 pattern
-rw-rw-rw- 1 user user   86 Mar 28 01:41 somefile.file

However, when I use echo `ls -l` > file1.txt, it has the same thing as ls -l normally but all in one line, where each line is separated by spaces and not new lines e.g.:

total 12K drwxrwxrwx 1 user user 4.0K Mar 27 22:45 . drwxr-xr-x 1 user user 4.0K Mar 27 22:45 .. -rw-rw-rw- 1 user user 4.5K Mar 23 16:27 books.xml -rw-rw-rw- 1 user user 0 Mar 13 00:19 file1 -rw-rw-rw- 1 user user 513 Mar 13 00:19 file2.txt -rw-rw-rw- 1 user user 2.8K Mar 13 12:42 file3.txt -rw-rw-rw- 1 user user 403 Mar 27 22:45 ls_l.txt -rw-rw-rw- 1 user user 89 Mar 13 00:18 pattern

Note that I have these relevant aliases:

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls -lah'
user12055579
  • 113
  • 4
  • 1
    Maybe there is an issue with lines needing to end in CR NL in windows. Are you using some Windows text editor to look at the file ? Which one? –  Apr 05 '20 at 04:15
  • Nope, I am using vim in the terminal. – user12055579 Apr 05 '20 at 04:16
  • I forgot how I somehow got ls -l > a1.file to be put in file with only one line. I think I may have piped something (as an issue seen here: https://unix.stackexchange.com/questions/10421/output-from-ls-has-newlines-but-displays-on-a-single-line-why) but I still don't know how I would pipe output from ls -l to be put in a file. Is that even possible? – user12055579 Apr 05 '20 at 04:18
  • It is difficult to help you if you can not reproduce the problem nor remember what you did to get into the problem in the first place. I don't know what to recommend. –  Apr 05 '20 at 04:20
  • Oh yes, I remember now. It is because I used echo `ls -l` > file1.txt. Why does it put it on one line only? – user12055579 Apr 05 '20 at 04:26
  • Sorry but the command echo ls -l | file1.txt won't provide a list of files, nor write to file1.txt and in fact it is a syntax error in any shell. Maybe your memory is playing tricks with you? –  Apr 05 '20 at 04:30
  • @Isaac Sorry, fixed! – user12055579 Apr 05 '20 at 04:30
  • Nope, . `ls -l` > file1.txt should not work either in most cases (unless the first file listed in the present directory is the name of an executable, and, in this case, must be named ls -l). Are you sure that that is the question you want answered? –  Apr 05 '20 at 04:34
  • Not sure what you mean? You first code in your comment is not what I have in my previous comment. I have: echo `ls -l` > file1.txt. – user12055579 Apr 05 '20 at 04:36
  • Sorry, misread yor comment, question answered as asked. –  Apr 05 '20 at 04:52

1 Answers1

4

Quoting !!

When you execute ls -l, yes, it generates newlines (as you report). When you do echo `ls -l` , however there are no newlines to be seen. Try it.

The reason is that a `...` construct is a command expansion, and most expansions (there are others) are affected by shell splitting (when not quoted). Shell splitting removes any of the characters in the variable IFS, which, by default, contains <space><tab><newline>. All newlines from ls -l get removed and the string gets split at that point. Latter, echo gets to join each split part separating them with a single space.

So, yes, both, the old, and the correct and preferred at any time new one:

echo `ls -l`
echo $(ls -l)

Will list files in the current directory in one string without newlines if IFS is default.

Redirecting that string to a file, will write one string to that file, as should be expected.

echo $(ls -l) > file

To retain the newlines quote the expansion:

echo "$(ls -l)"

Or, please avoid using the deprecated `...`:

echo "`ls -l`"

And to get the output in a file, just redirect it to the file:

echo "$(ls -l)" > file