Skip to content

Commit

Permalink
added process substituion
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyloris committed Feb 27, 2024
1 parent 75f0a0b commit 1e093e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mergeFiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Merge MD Files
run: |
# Define the order of markdown files to merge
files_to_merge=("index.md", "navigation.md", "files.md", "ssh.md", "sed.md", "grep.md", "searchingFiles.md", "netstat.md", "lsof.md", "curl.md", "wget.md", "redirection.md")
files_to_merge=("index.md", "navigation.md", "files.md", "ssh.md", "sed.md", "grep.md", "searchingFiles.md", "netstat.md", "lsof.md", "curl.md", "wget.md", "redirection.md", "process-substitution.md")
# remove existing contents readme
echo '' > README.md
# Merge files in the specified order
Expand Down
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

### Stuff you should to Know
1. [Redirection](#redirection)
2. [Process Substitution](#PROCESS_SUBSTITUTION)
2. [Process Substitution](#process-substitution)
3. [Bash Fu](#BASHFU)
11 changes: 11 additions & 0 deletions process_substitution.md
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)
```
23 changes: 20 additions & 3 deletions redirection.md
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).

0 comments on commit 1e093e0

Please sign in to comment.