Search code examples
bashubuntubatch-filecmdwindows-subsystem-for-linux

Is it possible to make a .bat Bash hybrid?


In cmd, it is possible to use Linux commands with the ubuntu or bash commands, but they are very fickle. In batch, it is also possible to make a VBScript-batch hybrid, which got me thinking, is it possible to make a Bash-batch hybrid? Besides being a tongue-twister, I feel that Bash-batch scripts may be really useful.


What I have tried so far

  • So far I tried using the empty bash and ubuntu commands alone since they switch the normal command-prompt to the Ubuntu/Bash shell, but even if you put commands after the ubuntu/bash they wouldn't show or do anything.

  • After I tried that, I tried using the ubuntu -run command, but like I said earlier, it’s really fickle and inconsistent on what things work and what things don't. It is less inconsistent when you pipe things into it, but it still usually doesn't work.

  • I looked here since it seemed like it would answer my question and I tried it, but it didn't work since it required another program (I think).

  • I also looked to this and I guess it failed miserably, but interesting concept.

  • What I've gotten from all of my research is that most people think when this is mentioned of a file that could be run either as a .bat file or as .sh shell file instead of my goal, to make a file that runs both batch and Bash commands in the same instance.

What I want this for relates to my other question where I am trying to hash a string instead of a file in cmd, and you could do it with a Bash command, but I would still like to keep the file as a batch file.


Solution

  • Sure you can use Bash in batch, assuming it is available. Just use the command bash -c 'cmd', where cmd is the command that you want to run in Bash.

    The following batch line pipes the Hello to cat -A command that prints it including the invisible symbols:

    echo Hello | bash -c "cat -A"
    

    Compare the output with the result of the version completely written in Bash:

    bash -c "echo Hello | cat -A"
    

    They will slightly differ!