How to Make Brainfuck Programs Executable

- On Linux, Unix and Variants

If you have ever either done any Brainfuck programming or just used tools written in Brainfuck, you will know that you have to manually invoke your favourite Brainfuck virtual machine (interpreter) each time you run a program. You may have typed something similiar to the following command several times:

./~/progs/bf MyProgram.b 

You may have placed the interpreter in your run-path or created an alias in your favourite shell, to simplify the above to:

bf MyProgram.b

This does not necessarily need to be the limit to what you can do. There is a simple way that all Brainfuck programs can be modified so that all you need to type to run any given Brainfuck program is:

./MyProgram.b

Or even better, if you rename your program removing the .b extension you will just need to type:

./Myprogram

And no one will ever need to know what language the program is written in, and there are several other benefits.

All Brainfuck tools, utilities and games on this site are executable. [see step 3 below]

How This Is Done

Step 1 : Setting up the interpreter

First you need to pick your favourite Brainfuck virtual-machine (interpreter) and ensure that it does not support the debugging command '#' that is not a standard part of the language. To find out if your current interpreter is unsuitable, download and run the debugtest.b program. If this program generates any output then you will need to find an interpreter which does not have this non-standard feature. If the program generates no output then your interpreter is suitable and you can continue.

When you have a suitable interpreter you will need to place it in a sensible location on your machine. I recommend that a copy of the interpreter, renamed if necessary to 'bf', be placed in /usr/bin - as this is a nice standard place to put things such as Brainfuck interpreters.

Your interpreter should have the appropriate file permissions to allow users to execute it. To do this you can issue the following two commands from a root shell:

chown root:root /usr/bin/bf
chmod 755 /usr/bin/bf

If you do not have system administrator access to your machine then you can instead place the interpreter somewhere you do have access to, or ask your system administrator for assistance. You must ensure that your interpreter is somewhere in the run path.

Once your interpreter is in place you, and hopefully any user of your system, can now type the following command to run a Brainfuck program:

bf MyProgram.b

Step 2 : Modifying Your Brainfuck Program

To do this you need to add the following line, followed by a blank line, to the start of any program you wish to make executable:

#!/usr/bin/bf

For example, the following 'Hello World' program:

>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<
++++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.
[-]>++++++++[<++++>-]<+.[-]++++++++++.+++.

Would become:

#!/usr/bin/bf
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[< ++++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------. [-]>++++++++[<++++>-]<+.[-]++++++++++.+++.

Once you have added the above header your program is almost executable, there is just one more step.

Step 3 : Making Your Brainfuck Program Executable

Your interpreter has been installed in a nice location, your Brainfuck program has been prepared, now lets make it executable by issuing the following command:

chmod 755 MyProgram.b

You may, at this point, drop the '.b' file extension completely as it is completely superflous.

Step 3.5 : Running Your Program

Simply type:

./MyProgram.b

Or, if you dropped the '.b' file extension:

./MyProgram

Congratulations, you have now created your first executable Brainfuck program.

Benefits of Executable Brainfuck Programs

Here is a list of the benefits of making your Brainfuck programs executable:

[1] Interpreters which use '#' as a special debugging opcode will generate a register dump on encountering the initial '#'. If yours is one of these then I recommend that you look for an alternative interpreter for day-to-day use.

[2] Your program should not request any user input as none can be received. If you want to write CGI scripts in Brainfuck that require input, you should use mod_bf instead.