How to run SWI-Prolog from the command line? How to run SWI-Prolog from the command line? shell shell

How to run SWI-Prolog from the command line?


ISO directive: initialization. This should work.

:- initialization main.main :-  write('Hello World\n').

edit sorry, I skipped over most interesting details. Here is a sample script, let's say saved in ~/test/main.pl

#!/home/carlo/bin/swipl -f -q:- initialization main.main :-  current_prolog_flag(argv, Argv),  format('Hello World, argv:~w\n', [Argv]),  halt(0).

and made executable with

chmod +x ~/test/main.pl

then I get

~$ ~/test/main.plHello World, argv:[]~$ ~/test/main.pl as,dnj asdlHello World, argv:[as,dnj,asdl]

In script main.pl, I used the swipl path that results from building from source without admin privileges. The SWI-Prolog build process put bin and lib under ~/bin and ~/lib

Note: the -f flag disables loading the initialization ~/.plrc, and this could be necessary to get more 'strict control' over execution...

I'm currently unsure if the documentation page is up-to-date with current SW status. From some mailing list message, and my own efforts to reuse thea, seems that command line flags changed recently...


The other answer is more or less correct, however, how it works might depend on your OS. The most portable way to do it is, appartently:

$ cat example.pl#!/path/to/your/swipl:- set_prolog_flag(verbose, silent).:- initialization main.main :-    format('Example script~n'),    current_prolog_flag(argv, Argv),    format('Called with ~q~n', [Argv]),    halt.main :-    halt(1).

The difference here is that there is nothing on the shebang line apart from the path to swipl. Everything else is done using directives. On my OS, only that worked!

$ chmod u+x example.pl$ example.pl foo bar bazExample scriptCalled with [foo,bar,baz]

EDIT

It is probably cleaner to remove the shebang line altogether, and instead run from the command line:

$ swipl -s example.pl -- foo bar bazExample scriptCalled with [foo,bar,baz]

Again, using a directive to set main/0 as the initialization goal saves you from having to explicitly do this on the command line. Calling swipl from the command line, on the other hand, lets your OS find where the executable is, instead of hard-coding this information in the script.


You can use initialization/2.

#!/path/to/swipl -q:- initialization(main, program).main :-    write("Hello"), nl,    halt.

Rationale:

  • -q (--quiet) is used to suppress all informational messages, including informational messages that originate from the SWI-Prolog initialization file (~/.swiplrc). :- set_prolog_flag(verbose, silent). could be used instead, but it does not suppress informational messages that come from the initialization file.

  • :- initialization(main, program). - program causes Prolog to exit with an error code on failure of main or on encountering an exception, rather than staying in the REPL.

  • halt - Stops the program with exit code 0.

The above assumes that you wish to load the initialization file (~/.swipl) before running the program. If you do not want to load the initialization file, use #!/path/to/swipl -f -q as the shebang. This makes the script start up faster.

(Also, be sure to set the executable bit (chmod +x hello.pl) before running the script (./hello.pl))