Yes, in the later the loop runs in a subshell too. That's unfortunately all you can get out of non-bash. It's a bit of pain, but since the parent and child are mostly equivalent, often you can just move the rest of the script into the receiving command in the pipeline.
Of course the bash solution works in other shells like zsh too. Olaf Dietsche Olaf Dietsche A variable that captures the data in a subshell is not visible to the parent shell. More synchronization is required in this case. I clarified my answer. If you already know the answer to your question, why are you asking in the first place? Thanks for your answers and clarification. No I have not found an answer yet. The variable way may not work since I want the subshell to use STDOUT to print something else, while at the same time pass some data to the parent.
Shouldn't process substitution be enough here? Show 3 more comments. Duplication of file descriptors in redirection Ask Question. Asked 6 years, 1 month ago. Active 6 years, 1 month ago. Viewed 6k times. Can you give some examples? Improve this question. Gilles 'SO- stop being evil' k gold badges silver badges bronze badges. Tim Tim 1. Add a comment. This construct allows both the standard output file descriptor 1 and the standard error output file descriptor 2 to be redirected to the file whose name is the expansion of word.
If it does, other redirection operators apply see Duplicating File Descriptors below for compatibility reasons. This construct allows both the standard output file descriptor 1 and the standard error output file descriptor 2 to be appended to the file whose name is the expansion of word.
This type of redirection instructs the shell to read input from the current source until a line containing only word with no trailing blanks is seen. All of the lines read up to that point are then used as the standard input or file descriptor n if n is specified for a command. No parameter and variable expansion, command substitution, arithmetic expansion, or filename expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word , and the lines in the here-document are not expanded.
This allows here-documents within shell scripts to be indented in a natural fashion. You can simply do this:. Bash's built-in read command reads a single line from standard input. At this point bash passes the all the input read so far to the stdin of the command. Here is a common example. A quick one-liner to do this would be:. For example, let's say you quickly want to pass the text in your clipboard as the stdin to a command. Instead of doing something like:.
This one-liner uses the built-in exec bash command. Running commands after setting up this redirect will have the stderr of all of them redirected to file. In general exec can take an optional argument of a command. If it's specified, bash replaces itself with the command. So what you get is only that command running, and there is no more shell. What this does is opens the file for reading and assigns the opened file-descriptor to the shell's file descriptor number 3.
The file descriptor table now looks like this:. Or you can use regular shell commands such as grep and operate on file descriptor 3 :. What happens here is file descriptor 3 gets duplicated to file descriptor 1 - stdin of grep. Just remember that once you read the file descriptor it's been exhausted and you need to close it and open it again to use it. You can't rewind an fd in bash. Here the file descriptor 3 is duped to - , which is bash's special way to say "close this fd".
Here we simply tell bash to open file for writing and assign it number 4. The file descriptor table looks like this:. As you can see file descriptors don't have to be used in order, you can open any file descriptor number you like from 0 to The diamond operator opens a file descriptor for both reading and writing.
This one-liner uses the commands construct that runs the commands a sub-shell. A sub-shell is a child process launched by the current shell. So what happens here is the commands command1 and command2 get executed in the sub-shell, and bash redirects their output to file.
Now take a look in shell 1. It will execute echo test. You can keep writing commands to fifo and shell 1 will keep executing them. In shell 1 we use the mkfifo command to create a named pipe called fifo. A named pipe also called a FIFO is similar to a regular pipe, except that it's accessed as part of the file system. It can be opened by multiple processes for reading or writing. When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the file system.
Thus, the FIFO special file has no contents on the file system; the file system entry merely serves as a reference point so that processes can access the pipe using a name in the file system.
Now in shell 2 we open the named pipe for writing and assign it a custom file descriptor 3. Next we simply write echo test to the file descriptor 3, which goes to fifo. It doesn't need to exist on your system. This special file is for opening tcp connections through bash.
And then we simply read the response back from the same file descriptor by using cat.
0コメント