Thursday, May 2, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 9723  / 2 Years ago, thu, march 31, 2022, 10:48:34

I was reading the O'Reilly book "Unix in a nutshell", and I saw there that commands can be grouped and executed in the current shell using the following format:



{ cmd1 ; cmd2 }


I didn't understand how this is possible, as I knew that a call to exec replaces a shell's data, and therefore the shell can't return from an exec system call.
So I tested the command and I can't understand what happened. I entered into a command line and I'm not sure to what does the command line belong.



Is it just the terminal emulator's command line, stripped down from the shell?



Also, how is executing commands in the current shell in any way useful?



enter image description here


More From » command-line

 Answers
0

To exec or not to exec




a call to exec replaces a shell's data, and therefore the shell can't return from an exec system call.




But not every command is the name of a program to execute. So often there is no exec call involved. The key distinction is that in contrast to ( cmd1; cmd2; ), you don't get another shell process. Or perhaps more correctly, the shell will only fork to execute child programs, not to execute shell processes.



Strange prompt




what does the command line belong.




As other answers have pointed out, your command was incomplete. So the command line belonged to bash itself, which was waiting for the end of your group.



Uses




Also, how is executing commands in the current shell in any way useful?




… as opposed to subshell grouping



The most prominent example is setting variables in that subcommand. Thy this:



foo=1; { foo=2; echo $foo; }; ( foo=3; echo $foo; ); echo $foo


This will print



2
3
2


So although you set foo=3 inside one group, that group was executed in a subshell and its settings didn't propagate to the parent.



Another thing worth considering is current working directory. { cd foo; make; } will leave you in directory foo whereas ( cd foo; make; ) will not alter the current working directory of your main shell.



… as opposed to ungrouped commands



The grouping is mostly useful if you have other code outside, e.g. conditionals



test -f foo && { echo "Foo exists."; rm foo; }


or redirections



{ echo "Content of $PWD:"; ls; } | gzip > dirlist.gz

[#26259] Thursday, March 31, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ibuteking

Total Points: 35
Total Questions: 128
Total Answers: 138

Location: Indonesia
Member since Thu, Oct 1, 2020
4 Years ago
;