InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Saving or Grouping Output from Several Commands in bash
Bash

Saving or Grouping Output from Several Commands in bash

InfinityCoder January 12, 2017

You want to capture the output with a redirect, but you’re typing several commands on one line.

1
$ pwd; ls; cd ../elsewhere; pwd; ls > /tmp/all.out

The final redirect applies only to the last command, the last ls on that line. All the other output appears on the screen (i.e., does not get redirected).

Use braces { } to group these commands together, then redirection applies to the output from all commands in the group. For example:

1
$ { pwd; ls; cd ../elsewhere; pwd; ls; } > /tmp/all.out

Alternately, you could use parentheses ( ) to tell bash to run the commands in a subshell, then redirect the output of the entire subshell’s execution.

For example:

1
$ (pwd; ls; cd ../elsewhere; pwd; ls) > /tmp/all.out

While these two solutions look very similar, there are two important differences. The first difference is syntactic, the second is semantic.

Syntactically, the braces need to have whitespace around them and the last command inside the list must terminate
with a semicolon.

That’s not required when you use parentheses. The bigger difference, though, is semantic—what these constructs mean.

The braces are just a way to group several commands together, more like a shorthand for our redirecting, so that
we don’t have to redirect each command separately.

Commands enclosed in parentheses, however, run in another instance of the shell, a child of the current shell
called a subshell.
The subshell is almost identical to the current shell’s environment, i.e., variables, including $PATH, are all the same, but traps are handled differently .

Now here is the big difference in using the subshell approach: because a subshell is used to execute the cd commands, when the subshell exits, your main shell is back where it started, i.e., its current directory
hasn’t moved, and its variables haven’t changed.
With the braces used for grouping, you end up in the new directory (../elsewhere in our example).

Any other changes that you make (variable assignments, for example) will be made to your current shell instance.

While both approaches result in the same output, they leave you in very different places.
One interesting thing you can do with braces is form more concise branching blocks . You can shorten this:

1
2
3
4
5
6
7
if [ $result = 1 ]; then
   echo "Result is 1; excellent."
   exit 0
else
   echo "Uh-oh, ummm, RUN AWAY! "
   exit 120
fi

into this:

1
2
3
[ $result = 1 ] \
  && { echo "Result is 1; excellent." ; exit 0; } \
  || { echo "Uh-oh, ummm, RUN AWAY! " ; exit 120; }

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Isolating Specific Fields in Data in bash
You need to extract one or more fields from each …

Isolating Specific Fields in Data in bash

Writing Output but Preserving Spacing in bash
You want the output to preserve your spacing. Enclose the …

Writing Output but Preserving Spacing in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap