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
Connecting Two Programs by Using Output As Arguments in bash
Bash

Connecting Two Programs by Using Output As Arguments in bash

InfinityCoder January 12, 2017

What if one of the programs to which you would like to connect with a pipe doesn’t work that way?

For example, you can remove files with the rm command, specifing the files to be removed as parameters to the command:

1
$ rm my.java your.c their.*

but rm doesn’t read from standard input, so you can’t do something like:

1
find . -name '*.c' | rm

Since rm only takes its filenames as arguments or parameters on the command line, how can we get the output of a previously-run command (e.g., echo or ls) onto the command line?

Use the command substitution feature of bash:

1
2
$ rm $(find . -name '*.class')
$

The $( ) encloses a command that is run in a subshell. The output from that command is substituted in place of the $( ) phrase.

Newlines in the output are replaced with a space character (actually it uses the first character of $IFS, which is a space by default, during word splitting), so several lines of output become several parameters on the command line.
The earlier shell syntax was to use back-quotes instead of $( ) for enclosing the subcommand.
The $( ) syntax is preferred over the older backward quotes syntax because it easier to nest and arguably easier to read.

However, you will probably see more often than $( ), especially in older scripts or from those who grew up with
the original Bourne or C shells.
In our example, the output from find, typically a list of names, will become the arguments to the rm command.

Warning: be very careful when doing something like this because rm is very unforgiving.
If your find command finds more than you expect, rm will remove it with n recourse. This is not Windows; you cannot recover deleted files from the trashcan.
You can mitigate the danger with rm -i, which will prompt you to verify each delete. That’s OK on a small number of files, but interminable on a large set.
One way to use such a mechanism in bash with greater safety is to run that inner command first by itself.

When you can see that you are getting the results that you want, only then do you use it in the command with back-quotes.
For example:

1
2
3
4
5
$ find . -name '*.class'
First.class
Other.class
$ rm $(find . -name '*.class')
$

We’ll see in an upcoming recipe how this can be made even more foolproof by using !! instead of retyping the find command .

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Getting bash Without Getting bash
You want to try out a shell or a shell …

Getting bash Without Getting bash

Testing Scripts in VMware in bash
You need to develop cross-platform scripts but do not have …

Testing Scripts in VMware 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

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

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 © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap