# Bash part echo Hello from bash!echo Normal syntax for variable expansion doesnt work here: %a%echo However, using syntax \"\$name\" we can expand even CMD.EXE variables: "\$A"=$Aecho Note: variables are case-sensivite, CMD.EXE variables are uppercaseecho Other features from bash should also workexit

D:\CMD\my\bash_hybrid.bat: line 1: @goto: command not foundHello from bash!Normal syntax for variable expansion doesnt work here: %a%However, using syntax "$name" we can expand even CMD.EXE variables: $A=bNote: variables are case-sensivite, CMD.EXE variables are uppercaseOther features from bash should also work

End of bash part

If someone finds a better way to make hybrid batch-bash files, please post it here.

How cmd interprets this:The colon symbol is the marker of cmd label so the rest of the line shouldn't be executed but SUB (ascii26) symbol is considered as line delimiter so the rest of line after SUB char is the normal command that will be interpreted and executed.

How bash interprets this:The colon symbol is the empty command with the following hash symbol that is the marker of bash comment, so the rest of the line will never be executed.

In summary:cmd executes first 5 lines (until goto :EOF) and exits. One of this lines has the command invoking bash with the same file as the bash script. Bash executes all lines but the first 5 lines are empty commands and comments and do not produce any useful work. The rest of the file is assumed as the fruitful bash script that is executed and does something useful.

1. The first line is assumed by cmd.exe as a label and is not interpreted the rest of the line after the colon. Further code is considered as valid batch script until the ending goto :EOF. This part invokes the same file as a bash script, as well

2. The colon symbol in bash is empty command and does nothing; following <<____CMD____ is the beginning marker of heredoc until the ending marker ____CMD____. The full content of heredoc is assumed as the argument of the command colon. So it does nothing in bash, as well. The rest of the file is interpreted as valid bash script.

Assumptions and limitations:-- bash is assumed in %PATH%-- batch prolog shouldn't have any duplicates of the heredoc markers (I guess ____CMD____ is good candidate to be a marker)-- batch part shouldn't have any bash command substitutions, for example, $( ... ), or ` ... `

jfl wrote:Also the batch file contains CRLF line endings, which make Ubuntu bash spit an error on each line.

The batch only contains CRLF endings, if you use an editor (for example notepad.exe) that use this line ending.So just use another editor, that stores only LF endings - the batch will still run without errors.

jfl wrote:But this fails with Error: 0x80070057.Any idea as to why this does not work?

The Linux subsystem for Windows 10 64 bit is still under developement, so there might be changes on every windows update.One issue i know is, that enabling 'Legacy mode' of cmd.exe will disable the Linux subsystem,so you have to deactivate this mode in the cmd properties (and restart cmd.exe - maybe a reboot is needed).

Sidenote:I still haven't tested my solution (don't have win 10 64 Bit), but i thought OperatorGK has tested 'bash "%~f0"' successfully (see: Output in the OP),so i'm a bit confused that this doesn't work for you, so i wonder if this command line option has been removed by one of the last windows updates,or if you have also have manually installed "external executables" which are executed instead of the 'windows featured bash'(although i think this is improbable because of the error you have got).

jfl wrote:[*] Also the batch file contains CRLF line endings, which make Ubuntu bash spit an error on each line. Resolved by using `tr -d '\r'` and piping the output to a second instance of bash.[/list]

Hmm. I don't like that. We are working in a Windows Environment which uses CRLF as its standard. Why would they not account for this in the version of BASH they are using on Windows.