Bast Hack

Bast is a technique for combining bash and batch script into a single file. Using a particular combination of commands, different code paths will be executed based upon which interpreter is used. This makes bast ideal for easily deploying build scripts and similar types of files, avoiding the need to distribute, for example 'build.sh' for *nix and 'build.bat' for Windows.

Bast is *not* a way to run batch file using bash, or vice versa. A file still needs to contain both bash and batch code, one after the other.

Here's the key component of bast:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 : # This is a bast file.
: <<BAST_HEADER
@echo off
goto batch_file
BAST_HEADER
#bash_file
echo "This is bash code."
...
exit
:batch_file
echo This is batch code.
...

A breakdown of code:

Line 1 is just a comment. This is optional.

Line 2 is the BAST_HEADER which begins the split between bash and batch. In batch, the ":" character begins a comment, so the batch interpreter ignores this line and begins executing at line 3. In bash, the ":" character is the "true" command, which is a no-op. The << operator creates a here doc, which is just string content that is piped into the true command. The net effect is that bash ignores lines until line 6, continuing execution there.

Line 3 is only executed by batch. It turns off echoing of commands, so batch will not output each line as it is executed.

Line 4 is only executed by batch. It causes batch to jump to line 10, where the label batch_file is defined.

Line 5 ends the here doc than began on line 2.

Line 6 is only executed by bash. It is only a comment to signify where bash begins execution. It is optional.

Lines 7 and line 8 are where the bash code goes.

Line 9 ends bash execution so that the bash interpreter doesn't try to run the batch code.

Line 10 is only executed by batch. It defines the label batch_file that line 4 jumps to.

Lines 11 and line 12 are where the bash code goes.

Batch execution is ended by the end of the file.

Paragrasp uses the bast hack for its own platform independent build script. It is recommended that the extension .sh.bat is used, since Windows cares about the extension ending in .bat, while *nix does not care about the exact extension.