-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## Process Substitution | ||
> What process substitution does is, it executes a command in subshell, and returns the stdout of that subshell as a file descriptor. We can make the file descriptor either readable or writable. | ||
```shell | ||
# read something from substituion | ||
read < <(echo "HELLo") # provides a readable fd | ||
cat <(echo "hey" "whatsup") | ||
|
||
# write something to it | ||
cat file.txt > >(cat) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
## Redirection | ||
|
||
> Redirect output to stdin of another command using `>` and to redirect standard input, we use `<`. | ||
> Redirect Stdout to some other file or redirect stdin to some otherfile. | ||
```shell | ||
cat file.txt > file2.txt | ||
# redirect stdout | ||
ls / > output.txt | ||
# redirect stdin | ||
cat < file.txt | ||
# redirect stderr | ||
find / 2> /dev/null | ||
# redirect stdout and stderr | ||
find / 2> /dev/null 2>&1 | ||
# in short we may use this to redirect both | ||
find / &> /dev/null | ||
|
||
|
||
# truncate a file or create a new one | ||
> newfile.txt | ||
# append to a file | ||
cat hi.txt >> greet.txt | ||
``` | ||
|
||
> To redirect output of one command to input of another, we use piping `|`. | ||
```shell | ||
ls / | grep root | ||
``` | ||
* We cannot do this using `>` because, it writes to a file descriptor and `grep` takes in a file descriptor to read from. To make it work, we may use process substitution. | ||
* We cannot do this using `>` because, it writes to a file descriptor and `grep` takes in a file descriptor to read from. To make it work, we may use process substitution. | ||
|
||
> Commands which are executed in pipes, are actually executed in subshells, with each command in its own subshell. To doing any variable assignments in the environment will not persist, rather be lost. | ||
```shell | ||
ls / | grep root | sed -i 's/hi/hello/g' | ||
``` | ||
|
||
> To overcome this limitation, we can use [Process Substitution](#process-substitution). |