The toplevel system is started by the command ocaml, as follows:
ocaml options objects # interactive mode
ocaml options objects scriptfile # script mode
options are described below.
objects are filenames ending in .cmo or .cma; they are
loaded into the interpreter immediately after options are set.
scriptfile is any file name not ending in .cmo or .cma.
If no scriptfile is given on the command line, the toplevel system
enters interactive mode: phrases are read on standard input, results
are printed on standard output, errors on standard error. End-of-file
on standard input terminates ocaml (see also the #quit directive
in section 9.2).
On start-up (before the first phrase is read), if the file
.ocamlinit exists in the current directory,
its contents are read as a sequence of Objective Caml phrases
and executed as per the #use directive
described in section 9.2.
The evaluation outcode for each phrase are not displayed.
If the current directory does not contain an .ocamlinit file, but
the user's home directory (environment variable HOME) does, the
latter is read and executed as described below.
The toplevel system does not perform line editing, but it can
easily be used in conjunction with an external line editor such as
ledit, ocaml2 or rlwrap
(see the
Caml Hump).
Another option is to use ocaml under Gnu Emacs, which gives the
full editing power of Emacs (command run-caml from library inf-caml).
At any point, the parsing, compilation or evaluation of the current
phrase can be interrupted by pressing ctrl-C (or, more precisely,
by sending the INTR signal to the ocaml process). The toplevel
then immediately returns to the # prompt.
If scriptfile is given on the command-line to ocaml, the toplevel
system enters script mode: the contents of the file are read as a
sequence of Objective Caml phrases and executed, as per the #use
directive (section 9.2). The outcome of the
evaluation is not printed. On reaching the end of file, the ocaml
command exits immediately. No commands are read from standard input.
Sys.argv is transformed, ignoring all Objective Caml parameters, and
starting with the script file name in Sys.argv.(0).
In script mode, the first line of the script is ignored if it starts
with #!. Thus, it should be possible to make the script
itself executable and put as first line #!/usr/local/bin/ocaml,
thus calling the toplevel system automatically when the script is
run. However, ocaml itself is a #! script on most installations
of Objective Caml, and Unix kernels usually do not handle nested #!
scripts. A better solution is to put the following as the first line
of the script:
#!/usr/local/bin/ocamlrun /usr/local/bin/ocaml