Answers for "bash script exec blocks"

0

bash script exec blocks

Instead of ( something ), which launches something in a subshell, use { something ; }, which launches something in the current shell

You need spaces after the {, and should also have a ; (or a newline) before the }.

Ex:

$ { echo "hello $BASHPID";sleep 5;echo "hello again $BASHPID" ; }
hello 3536
hello again 3536
Please note however that if you launch some complex commands (or piped commands), those will be in a subshell most of the time anyway.

And the "portable" way to get your current shell's pid is $$.

So I'd instead write your test as:

{ echo "hello $$"; sleep 5 ; echo "hello again $$" ; }
(the sleep is not really useful anyway here)
Posted by: Guest on March-29-2021

Browse Popular Code Answers by Language