2

When I execute

echo `man ls` > tem.txt

I get unformatted output in the text file, I mean output without any new lines, just continues sentences. How do I get formatted output ?
For example, unformatted output looks like:

LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls
[OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the
current directory by default). Sort entries alphabetically if none of -
cftuvSUX nor --sort is speci‐ fied. Mandatory arguments to long options are
mandatory for short options too. -a.................
Alex Jones
  • 6,353

1 Answers1

3

You don't need to force man's output via process substitution. Redirection works fine for it:

man ls > tem.txt

Even if you so use process substitution, remember to use quotes, otherwise the output will undergo splitting + globbing from the shell:

echo "$(man ls)" > tem.txt
muru
  • 72,889
  • how does double quotes help ? – Alex Jones Dec 25 '14 at 12:59
  • globbing is performed on wildcards and commands right ? how on output ? – Alex Jones Dec 25 '14 at 13:00
  • 3
    @edwardtorvalds A command line is processed in several stages. Command substitution takes place before the globbing stage. So if the command's output contains wildcards, it will be expanded. Quoting works normally: prevents the shell from expanding wildcards and compressing and replacing all whitespace with spaces. – muru Dec 25 '14 at 13:10