I couldn't understand the difference between command grouping and pipelining
Asked
Active
Viewed 204 times
0
-
2Command grouping as in what ? You mean `foo ; bar` or `{ foo; bar;}` ? Can you provide specific commands that you're confused about ? – Sergiy Kolodyazhnyy Jan 22 '19 at 06:21
-
What is the difference between ( foo ; bar ), { foo ; bar ; } and foo | bar – karthik menon Jan 22 '19 at 07:02
-
6Possible duplicate of [When to use () vs. {} in bash?](https://askubuntu.com/questions/606378/when-to-use-vs-in-bash) and [Difference between | and ||](https://askubuntu.com/questions/798355/difference-between-and/798357) – Olorin Jan 22 '19 at 07:32
1 Answers
5
The differences between these are:
( foo; bar; )
It will execute the commands in a subshell, so if you made any changes in the subshell they will not appear outside the subshell. Like
i=2; ( ((i++)); echo $i ); echo $i
You will get output:
3
2
If you do the same thing in { } then it will be executed in the same environment, so the changes will matter. Like
i=2; { ((i++)); echo $i; }; echo $i
will give:
3
3
Now let's come to pipelining, pipelining is used to take input and give output to some commands.So the command:
a | b
the output of command a will be given as an input to command b.
echo "hi" | cat
will give you output hi.
So output of echo "hi" i.e. hi will be input of cat command.
Prvt_Yadav
- 444
- 8
- 17