What is the difference between spawn and exec?

Milan Babuškov picture Milan Babuškov · Feb 16, 2009 · Viewed 16.2k times · Source

I'm learning to write a TCL (expect) scripts and I notice that some examples show to use spawn, while others show the command exec. I tried googling, but can't find what is the difference?

Suppose I call 'exec' in a middle of a long expect script, what can I expect to happen?

Answer

Andrew Stein picture Andrew Stein · Feb 16, 2009

spawn is an expect command not a tcl command. exec is a tcl command.

spawn creates a process. The processes' input and output are connected to expect for use by the other expect commands: send, expect and interact.

exec creates a subprocess under tcl. In general the tcl is suspended until the subprocess completes. However, one can create the subprocess in the background (using & as the last argument) and if one hooks up the input and output correctly, tcl can interact with the subprocess. This is very clumsy and is exactly the sort of interaction that expect was designed to handle smoothly.