david
david

Reputation: 57

How can I concatenate the output of two commands?

How can I concatenate the output of pwd and ls and add it to a file called f1? This is not working:

pwd, ls > f1

Upvotes: 3

Views: 5440

Answers (3)

Zulu
Zulu

Reputation: 9285

Maybe you looking for doing something harder but file appending is a simple solution:

pwd >> f1
ls >> f1

If you prefer chepner's or Mark Setchell's answer, here's an explanation:

  1. You can use subshell with parenthesis:

    ( pwd; ls; ) > f1
    
  2. Or subcommand:

    { pwd; ls; } > f1
    

With subshell, parent shell won't have access to children environment. Variable aren't kept because a new orphan process is created.

And with subcommand initialized variable are kept and usable with parent.

Both have parent environment.

Reference:

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 208077

Saves a semi-colon and 2 spaces, costs a process vs @chepner :-)

(pwd;ls) > f1

Upvotes: 1

chepner
chepner

Reputation: 532418

Use a compound command:

{ pwd; ls; } > f1

Upvotes: 10

Related Questions