Use WSL(Windows Subsystem for Linux) bash command in CMD - bash

I am favorite user of WSL.
Before Restoring my Windows 10 OS, I setup WSL in my Windows and I didn't change any configuration. But One Day, the bash command were executed in CMD immediately. So, If I opened CMD and run a command such as 'ls', then result was the same in bash thing.
However now, I restore my windows and above command excuting no longer occurs. I often use bash commands, so I want to execute bash command in CMD. What should I do?

Related

Why is $OSTYPE returning "linux-gnu" on Windows?

Problem
When I run a bash script on Windows:
bash my_script.sh
I get linux-gnu for $OSTYPE inside the script.
Why is this like this?
I assume that i have WSL installed is relevant here.
Tested in PowerShell and CMD.
Git bash is returning msys like expected! Thx #user1934428
Background
I want to start some python scripts from bash, but not inside WSL.
From my command line I reach different python versions on windows, but from inside the bash scripts it is using the one inside WSL (except for GitBash).
You're right, running the bash command in PowerShell or CMD will launch WSL to run your script. You confirm this (and see which version of WSL) by running cat /etc/issue in your bash script. Your WSL environment will have an independent set of environment variables (not just $OSTYPE). You can see this by comparing the output of Get-ChildItem -Path Env:\ in PowerShell to the output of env (after you launch bash from PowerShell).
I suspect that the python version discrepancy you're seeing is a result of the PATH variable in your WSL runtime not matching what you have set in your PowerShell environment. You can fix your version issue by setting an alias containing a path to the python executable you want to use by adding alias python=/c/path/to/python.exe to the start of your bash scripts.
Alternatively, you can use a tool like Cygwin or git-bash to run your scripts. I'm not sure if they will use the same path variables as Windows so you may need to set those manually too.

WSL - Launch bash from Powershell in new terminal and pass a command to it

I am trying to make a simple Powershell script to quickly setup my dev environment. For that I need a few instances of WSL programs running on bash terminals.
From Powershell, I am trying to:
Open a new terminal window
Start bash
Run a command with bash - In my use case I just want to run a simple npm start within bash.
A plus is if I can do all of this in one script line.
I think I am close. If I use start powershell I can start a new terminal. That inmediately opens a new PowerShell terminal.
Then, I can pass PowersShell commands to it like so:
start powershell{bash}
This opens a new terminal window and immediately opens bash.
A way to pass commands to bash in PowerShell is like this:
bash -c "npm start"
This works well. It opens bash in the same terminal and then runs the command I am passing to it. npm start works just as if I was calling it directly from bash. The problem comes when I want to pass the npm start to the new terminal. This is what I am trying:
start powershell{ bash -c "npm start"; Read-host}
This opens the new powershell terminal and it seems to be opening bash. ; Read-host is added so that the terminal doesn't close immediately. However, instead of running the npm command, it reacts by showing me information about the npm command instead of actually running it.
Is there a workaround so that I can actually get the command to run in the new terminal window after opening bash?
I believe that the recommended way to start commands in the WSL environment is to use wsl.exe now. (https://learn.microsoft.com/en-us/windows/wsl/reference#wslexe)
Try start powershell{wsl -- npm start; Read-Host}

wsl executes only bash commands and not windows options

When I try to execute cmd commands such as wsl -l, wsl returns this message:
/bin/bash: -c: option requires an argument
However, using wsl as a bash command launcher works.
What do I have to do in order to use wsl windows options such as wsl -l, wsl --install, etc.?
Thanks in advance!
I assume that you've entered wsl command in the Linux terminal and when you did this the first time bash answered you that the command was not found , so you installed 'wsl' package. This is a completely different package. If you want to invoke Windows binaries from bash you should use .exe file extension. Try wsl.exe -l

How would I interface with the Linux terminal (bash for windows) using windows command prompt?

I just recently found out about using bash in windows. I had alot of fun installing linux programs onto my windows computer using bash and wondered how I would be able to run an automatic script so I dont have to export my display everytime I open bash.
I used to write scripts for cmd called batch scripting and I would be able to do everything cmd could do. Now that I have access to bash, I want to script a program that connects useful pieces of cmd with useful pieces of bash but I Can't find any results telling me how to call bash commands from cmd. Thank you - Zak Kaioken

how to write a batch (windows) to start Cygwin (mintty.exe) and then execute a python script?

Pretty much as titled. If I were to manually do this, I would first open a Cygwin Terminal (which should be /cygwin/bin/mintty.exe), and then in that terminal, cd to the directory that has the python script, and then execute the python script by doing "python myPython.py". I'm wondering if I can write a batch script or a bash script to do this: start a Cygwin Terminal, cd to a directory, execute a python script in the directory.
Thanks.
Edited:
So I have a python script that generates csv files for activities through mongodb.This script won't function if I run it through windows cmd. I have to run it in cygwin terminal (mintty.exe). So any alternatives to execute the python script won't work. I have to somehow start a Cygwin Terminal and execute the python script through there. Any ideas please? Thanks.
Depending of your needs it could be better to start mintty (creating a new window) instead of starting bash inside the cmd.exe window.
When you want to use ansi escape sequences then it works better with a real mintty window, as the cmd window ignores the escape sequences for window resizing and positioning.
start "" C:\cygwin\bin\mintty --exec ./myProgramToExecute.sh
You may start a bash from the Windows terminal and start your script from there (without starting mintty.exe). Just execute
bash -c "cd /your/directory && python myPython.py"
from the Windows cmd prompt or a batch file.

Resources