Here is a sample session. We use $ for the Unix shell prompt, and # for the Ocaml shell prompt.
$ rlwrap ocaml
Objective Caml version 3.12.0
# 3 + 5 ;;
- : int = 8
# print_string "hello world! \n" ;;
hello world!
- : unit = ()
#
Hit Ctrl-D or type exit 0;; at the prompt to exit the Ocaml shell.
# exit 0 ;;
[cs130s@ieng6-201]:~:260$
At the ocaml prompt enter #use "foo.ml" to load the file foo.ml (which must be present in the directory in which you ran the shell.). This has the same effect as typing out the file at the prompt. However, it is far more convenient to type and edit the file with Vim/Emacs/GEdit and load it with #use. Don’t forget to save the file in the editor before you (re)load it in the shell!
If you are familiar with Emacs, you can use the Emacs Mode
You can directly send the file foo.ml into OCaml and get the output piped to stdout by typing
$ ocaml foo.ml