Integrating Linux commands in Windows (10) CMD shells

0

On my previous (Win 7) machine I installed CygWin which allowed (due to PATH) me to call Linux commands from any command shells -- the default one, the Anaconda one, etc.

I would like to have the same ability on my new Win 10. I installed the Linux sub-system (Ubuntu), but I cannot see any way to allow Linux commands to be used in other shells?

Is there a way to achieve this, or should I just install CygWin?

mibm

Posted 2019-05-06T11:46:38.050

Reputation: 101

Read this blog post for detailed information.

– Biswapriyo – 2019-05-08T16:43:57.717

Answers

0

There are two ways that Linux utilities can be run on Windows:-

  • Compile the code as a native Windows .EXE file: this is what CygWin does, which is how all the individual utilities are available individually for direct invocation from a Windows shell.
  • Provide an environment within Windows in which Linux native ELF binaries can run, in a manner similar to how Linux runs Windows programs under Wine: this is what Windows Subsystem for Linux does, and the individual programs are not recognised as executables outside this environment.

The WSL environment is launched when bash.exe is run, and you can run WSL utilities by invoking through bash.exe, eg:

bash -c ls

If you need to pass parameters, you will need to quote the command string, eg:

bash -c 'ls -l ./Documents/'

I haven't checked CygWin, but some native Windows ports allow flexibility in their run strings (eg / instead of - for options, and \ instead of / for directory separators) - with WSL you will need to use strict Linux run strings.

AFH

Posted 2019-05-06T11:46:38.050

Reputation: 15 470

0

This is likely a duplicate of Executing linux programs (WSL) from Windows environment

As I answered in that question, to run Linux commands you'll need to invoke bash with the -c option. You also need to convert the path to the Linux version with wslpath if the path is not already in the Linux form. For example to run grep on E:\somefile then call it like this

bash -c "grep 'my string' $(wslpath 'E:\somefile')"

Do note that single quote isn't a special character in cmd.exe, therefore you must use double quotes in cmd. bash -c 'ls -l ./Documents/' like what AFH said won't work. OTOH in PowerShell both '' and "" are used for quoting, and the meaning is the same as in bash so you can call bash -c 'echo -e "some string"' from PowerShell without problem

However in PowerShell also has various Linux-like aliases like ls, cat, man... so in many cases you can use them without resorting to WSL bash

If you use Cygwin then every Unix utility will be compiled into a separate *.exe file. You can run which ls or which grep to get their paths easily. They're run directly in the Windows environment, not WSL which is a separate subsystem

phuclv

Posted 2019-05-06T11:46:38.050

Reputation: 14 930