%%script bash
echo 'hi there!'

"""
Explanation: Example Uses Of “%%script” Magic
The %%script cell magic allows the invocation of any number of external languages without the need for installing any custom Jupyter kernels. The drawback is that there is no context maintained between one cell invocation and the next. Another drawback is that the notebook interface insists on syntax-colouring the cell contents as though it were Python code.
The syntax is

%%script commandline

Simple example: bash
End of explanation
"""


%%script sbcl
(+ 2 2)

"""
Explanation: Use with SBCL:
End of explanation
"""


%%script sbcl --script
(+ 2 2) ; produces no output
(princ (+ 2 2)) ; need to do explicit output

"""
Explanation: You probably want to turn off the startup banner message, and the “*” characters from the default prompting for input by the REPL. The --script option does this, but it also turns off automatic output of expression values:
End of explanation
"""


%%script sbcl --noinform --eval "(require 'sb-aclrepl)"  --eval "(setq sb-aclrepl:*prompt* \"\")"
(+ 2 2)

"""
Explanation: This may be OK for running functional scripts, but it is not so convenient for simple experimentation.
Alternatively, the --noinform option turns off the startup banner. You can then change the prompt to the empty string by setting the SBCL-specific sb-aclrepl:*prompt* variable. To access this, you need to require the sb-aclrepl package. To avoid spurious output, this can all be done with --eval options on the command line:
End of explanation
"""


%%script sbcl --script
(setf + 2)
(princ (+ + +))

"""
Explanation: Just to make clear the fact that function names and variable names exist in separate name spaces: in the following example, “+” is used as the name of a variable and to refer to the built-in addition function.
End of explanation
"""


%%script sbcl --script /dev/stdin the quick brown fox
(print sb-ext:*posix-argv*)

"""
Explanation: Note that access to command-line arguments is not a standard feature of Common Lisp. SBCL provides an extension to do this:
End of explanation
"""


%%script sbcl --script
(defvar n 15) ; change as required
(do
	(
		(a 1 (+ a b))
		(b 1 a)
		(i 0 (+ i 1))
	)
	((>= i n))

	(format *standard-output* "~A~%" a)
) ; do


"""
Explanation: My example of computing the first n terms of a Fibonacci series:
End of explanation
"""


%%script sbcl --noinform --eval "(require 'sb-aclrepl)" --eval "(setq sb-aclrepl:*prompt* \"\")"

(setf (symbol-function 'counter)
    (let ((count 0))
        (lambda ()
             (if (> count 5)
                (setf count 0)
                (incf count)
             ) ; if
        ) ; lambda
    ) ; let
)

(counter)
(counter)
(counter)

"""
Explanation: Jake’s example of lexical binding in action:
End of explanation
"""


%%script bash
set -e

WORKDIR=$(mktemp -d)
cd "$WORKDIR"

cat >test.c <<EOD
#include <stdio.h>

int main(int argc, char **argv)
  {
    fputs("hello, world!\n", stdout);
  } /*main*/
EOD

gcc -o test test.c

./test

cd
rm -rfv "$WORKDIR"

"""
Explanation: A more elaborate %%script example: compiling and running an entire C program in a cell!
End of explanation
"""


