Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-14-2016, 02:38 PM   #1
Registered: Nov 2001
Posts: 126

Rep: Reputation: 15
Bash - Capture "Child" script output with "Parent" without using files or coproc keyword

I am aware of the easiest method of doing this by redirecting the child output to a file and then reading it with the parent.

I also know of using the Bash coproc keyword but I have not personally done it that way.

Is there an additional way of doing this besides the two listed methods? Say, if someone was using a Unix system without Bash 4 and without write access to the filesystem. I was also just curious. As always, thanks and have a good day.
Old 02-14-2016, 02:53 PM   #2
Registered: Jan 2009
Location: Seattle, WA
Distribution: Slackware64 14.2
Posts: 259

Rep: Reputation: 11
There is command substitution. E.g.,

ARCH=$(uname -m)
The $(...) is replaced by the output of the enclosed command.

Last edited by ahc_fan; 02-14-2016 at 02:54 PM.
1 members found this post helpful.
Old 02-14-2016, 07:51 PM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,873
Blog Entries: 4

Rep: Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578
Most commonly, this is done in the way that, say, "the bash 'pipe' ("|") operator does it." You simply arrange for the STDOUT and/or the STDERR handles of the child to be directed to a pipe, then open and read the pipe.

Although pipes "appear to be 'files,'" and can in fact be referred-to by names, they are actually inter-process communication queues that can be manipulated using ordinary fopen/fclose/fread/fwrite/fctl primitives "as though they were files."

Thus: "pipes, therefore also coproc," are(!) "the right way to do it," and "disk files" (despite the possible presence of 'file names,' and the apparent use of file-I/O primitives) are not actually involved.

A pipe, as I said, is a queue. When a reader reads, it is ordinarily "blocked" (put to sleep ...) until data is available to be read. Similarly, when a writer writes to a "full" pipe, it is blocked until the pipe is no longer full. All of this synchronization is completely invisible to the processes, who, so far as they can tell, are simply using "files." The idea was quite magical in the 1970's, and it's still quite magical today.

Last edited by sundialsvcs; 02-14-2016 at 07:58 PM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
"failed to execute child process" "Input/output error" fl.bratu Fedora 4 12-15-2008 04:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
getenv("LOGNAME") is incorrect when a child create a shell which call parent powah Programming 3 08-23-2007 12:29 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration