- 0.shell process substitution usage
- 1. Necessity of using the process to replace
- 2. Replace the nature of the process
And command substitution process is very similar. Alternatively command is the output of a command assigned to another variable , for example, dir_files=`ls -l`
or date_time=$(date)
; the process alternative is to pass the output of a command to another (group) command .
0.shell process substitution usage
Writing | meaning | important point | Nature |
---|---|---|---|
<(commands) | It means of the input redirection, its output may be used as another command input |
command list is a set of commands to command a plurality semicolon Note that, |
/ Dev / fd / n document acceptance (Commands) output, as another command input |
>(commands) | It means of output redirection, to accept another command output standard |
Reading from / dev / fd / n file as (Commands) input |
1. Necessity of using the process to replace
For Li
|
result
|
Explanation
|
---|---|---|
Normal mode
|
The output is empty | Shell command in the parent echo, whereas in the read command in the sub-Shell , When the read execution sub Shell is destroyed, AA variable disappears. Pipeline command is always executed in the sub-Shell, any command to assign values to variables will be met with this problem. |
Use process substitution
|
The output is aaaaa | On the whole, Shell the
Read data is saved to the current process variable AA, it is possible to successfully use echo output. |
Examples of processes used to replace "accepts standard input" in echo "qpy" > >(read; echo "你好,$REPLY") |
Run Results: Hello, qpy | Because of the use of the redirection, read commands from echo "qpy" the output of the read data. |
2. Replace the nature of the process
To be able to transfer data between different processes, in fact, the process of replacement will associate with the system files , the name of this file is /dev/fd/n
(n is an integer). The file is passed as an argument to ()
the command, ()
the command is to read or write to the file depends on the format of the replacement process is <
still >
:
- If so
>()
, then the file will give()
the command to provide input; With output redirection, what to enter may come from other commands. - If yes
<()
, then the file receives()
the output of the command; via the input redirection, the contents of the file can be used as input other commands.
Use the echo command to view the process of replacing the corresponding file name:
command | Export | Explanation |
---|---|---|
echo >(true) |
/dev/fd/63 |
/dev/fd/ There are a lot of numbers in the directory file, the process is generally used to replace 63 file, which is an internal file system, we generally see less.
So that data can flow between these commands. |
echo <(true) |
/dev/fd/63 |
|
echo >(true) <(true) |
/dev/fd/63 /dev/fd/62 |
|
echo "qpy" > >(read; echo "hello, $REPLY") | hello, qpy |
The first
|