What is the difference between running a program with exec command or not ?
For instance, If I made a script file like below.
#script1
python test.py
#script2
exec python test.py
Both seem to return the same result.
Are they equivalent?
exec is a shell built-in, which replaces the image of the current process with new process. It's not same as calling a binary/executable.
To see the difference, do:
#script1
python test.py
echo Hello
#script2
exec python test.py
echo Hello
You will not see the Hello getting printed in the second script.
exec also another purpose in shells. It can be used for redirection. For example,
exec 1>file
redirects the stdout of the process to file.
If you had:
exec 1>file
echo hello
echo world
then script would redirect hello and world to file instead of stdout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With