How can I concatenate the output of two commands?

How can I concatenate the output pwd

and ls

and add it to a file named f1? This does not work:

pwd, ls > f1

      

+3


source to share


3 answers


Use a compound command:



{ pwd; ls; } > f1

      

+8


source


Keeps half and 2 spaces, cost vs @chepner process :-)



(pwd;ls) > f1

      

+1


source


You may be looking for something more complex, but adding files is a simple solution:

pwd >> f1
ls >> f1

      

If you prefer Chepner's or Mark Setchell's answer, here's the explanation:

  • You can use a parenthesis subshell:

    ( pwd; ls; ) > f1
    
          

  • Or a subcommand:

    { pwd; ls; } > f1
    
          

With a subshell, the parent shell won't have access to the child's environment. The variable is not saved because a new orphan process is being created.

And with the subcommand, the initialized variable is preserved and can be used with parents.

Both have a parent environment.

Link:

+1


source







All Articles