|Did you know ...||Search Documentation:|
A Prolog source file can be used directly as a Unix program using the
#! magic start. The Unix
#! magic is
allowed because if the first letter of a Prolog file is
the first line is treated as a comment.12The
can be the legal start of a normal Prolog clause. In the unlikely case
this is required, leave the first line blank or add a header comment.
To create a Prolog script, use one of the two alternatives below as
first line. The first can be used to bind a script to a specific Prolog
installation, while the latter uses the default prolog installed in
#!/path/to/swipl #!/usr/bin/env swipl
The interpretation of arguments to the executable in the
HashBang line differs between Unix-derived systems. For
#! must be followed immediately with an
absolute path to the executable and should have none or one argument.
Neither the executable path, nor the argument shall use quotes or
spaces. When started this way, the Prolog flag argv
contains the command line arguments that follow the script invocation.
Starting with version 7.5.8, initialization/2
support the When options
allowing for the following definition of a Prolog script that evaluates
an arithmetic expression on the command line. Note that main/0
is defined lib the library
library(main). It calls main/1
with the command line arguments after disabling signal handling.
#!/usr/bin/env swipl :- initialization(main, main). main(Argv) :- concat_atom(Argv, ' ', SingleArg), term_to_atom(Term, SingleArg), Val is Term, format('~w~n', [Val]).
And here are two example runs:
% ./eval 1+2 3 % ./eval foo ERROR: is/2: Arithmetic: `foo/0' is not a function
Prolog script may be launched for debugging or inspection purposes
using the -l or -t. For example,
-l merely loads the script, ignoring
swipl -l eval 1+1 <banner> ?- main. 2 true. ?-
We can also force the program to enter the interactive toplevel after
the application is completed using
swipl -t prolog eval 1+1 2 ?-
The Windows version simply ignores the
versions extracted command line arguments from the HashBang
line. As of version 5.9 all relevant setup can be achieved using directives.
Due to the compatibility issues around HashBang line
processing, we decided to remove it completely.
Contra Carlo Capelli's note, the "shebang" is not at all overengineered.
As a commenter writes "It is probably cleaner to remove the shebang line altogether, and instead run from the command line"
On the other hand, if you DO use it:
You MUST have an absolute path in there because the part of the system (some part of the kernel) that starts the indicated command (=/usr/bin/env= or =/usr/local/bin/swipl0) doesn't know anything about the search paths currently set in your shell or in the process that exec's the script with the shebang.
If you absolutely need "relative paths", use the trick of passing via "env", which nasty and dangerous (because in effect, you are saying that whatever "swipl" is, you trust it; but it could be some old forgotten program, or a software nasty put on your search path with malice aforethought), but it works. Note that if you use "env", you can't pass arguments to the "swipl" executable anymore.
An excellent review answer is here:
More details than are needed here: