4 shell — Temporarily invoke operating system
You can now do whatever you need to do, and Stata will wait until you exit the window before
continuing.
Experienced Stata users seldom type out the word shell. They type “!”. Also you do not have
to enter your operating system, issue a command, and then exit back to Stata. If you want to execute
one command, you can type the command right after the word shell or the exclamation point:
. !mv try15.dta final.dta
Be careful because sometimes you will want to type
. !!vi myfile.do
and in other cases,
. winexec xedit myfile.do
!! is a synonym for xshell—a command different from, but related to, shell—and winexec is a
different and related command, too.
Before we get into this, understand that if all you want is a shell from which you can issue Unix
commands, type shell or !:
. !
mycomputer$
When you are through, type exit to the Unix prompt, and you will return to Stata:
mycomputer$ exit
.
If, on the other hand, you want to specify in Stata the Unix command that you want to execute,
you need to decide whether you want to use shell, xshell, or winexec. The answer depends on
whether the command you want to execute requires a terminal window or is an X application:
. . . does not need a terminal window: use shell . . . (synonym: !. . . )
. . . needs a terminal window: use xshell . . . (synonym: !!. . . )
. . . is an X application: use winexec . . . (no synonym)
When you type shell mv try15.dta final.dta, Stata invokes your shell (/bin/sh, /bin/csh,
etc.) and executes the specified command (mv here), routing the standard output and standard error
back to Stata. Typing !mv try15.dta final.dta is the same as typing shell mv try15.dta
final.dta.
When you type xshell vi myfile.do, Stata invokes an xterm window (which in turn invokes
a shell) and executes the command there. Typing !!vi myfile.do is equivalent to typing xshell
vi myfile.do.
When you type winexec xedit myfile.do, Stata directly invokes the command specified (xedit
here). No xterm window is brought up nor is a shell invoked because, here, xterm does not need
it. xterm is an X application that will create its own window in which to run. You could have
typed !!xedit myfile.do. That would have brought up an unnecessary xterm window from which
xedit would have been executed, and that would not matter. You could even have typed !xedit
myfile.do. That would have invoked an unnecessary shell from which xedit would have been
executed, and that would not matter, either. The important difference, however, is that shell and
xshell wait until the process completes before allowing Stata to continue, and winexec does not.