Search code examples
lispcommon-lispsbcl

Get the path of the script that is currently executing in Steel Bank Common Lisp


I'm calling a CL script from various places in the system. How do I get the file path of the script that is currently executed?

For example, the script source file is located in the /home/user/project/source/ directory. The script is being executed from the /home/user/ directory in the following manner:

user@machine:~$ ./project/source/script.lsp

Regardless of the callers location, script should know that it's located in the /home/user/project/source/ directory.

I've tried using the *default-pathname-defaults* variable, but the following command shows the directory from which the script was called:

(format t "Pathname: ~S~&" *default-pathname-defaults*)

Environment: SBCL 1.4.5.debian on Ubuntu 18.04.


Solution

  • The *load-truename* and *compile-file-truename* variables are bound to the truename of the file being loaded with cl:load or compiled with cl:compile-file at the time of loading or compilation, respectively.

    In your case, *load-truename* is the thing to use. It will give a full, absolute pathname to the script.