Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run *.cmd from wsl shell #4547

Closed
oHaiyang opened this issue Sep 26, 2019 · 5 comments
Closed

run *.cmd from wsl shell #4547

oHaiyang opened this issue Sep 26, 2019 · 5 comments
Labels

Comments

@oHaiyang
Copy link

oHaiyang commented Sep 26, 2019

Microsoft Windows [Version 10.0.18980.1]

The documents say one can Run Windows tools from WSL, if one type notepad.exe into the WSL shell, the Windows application should launch. But what if I want to run a cmd script directly from WSL shell, is that possible?

For some tools like yarn and npm, they do not have yarn.exe or npm.exe, but use a .cmd script as their command interface.

@archilkarchava
Copy link

archilkarchava commented Sep 26, 2019

You can make a wrapper bash/zsh/fish/sh script for it. Put cmd.exe /c <yourscript>.cmd inside your script.

@oHaiyang
Copy link
Author

Thanks, I will have a try and post the scripts for someone may have same situation.

@therealkenc
Copy link
Collaborator

The use case is hard to follow here though. Yep, cmd.exe /c is how you would invoke a .cmd script from WSL, but no Linux npm would ever do that. Conversely once you invoke npm.exe you are off in Windows-land, and anything in the npm.exe process tree after that happens in Windows. The Windows npm.exe will spawn whatever it needs to spawn, which might end up being some .cmd script; but WSL is oblivious.

But whatever the particulars of the use case, @archilkarchava has the canonical answer of how to evoke a .cmd script from WSL.

[Once some time back when someone else asked this question, I had wondered idly if anyone had done a cmd.exe interpreter look-alike in Linux. Never did find one. There is Linux pwsh, which is pretty cool.]

@oHaiyang
Copy link
Author

oHaiyang commented Oct 9, 2019

@archilkarchava Thanks again.

end up with a simple shell function that works great for me:

cmd() {
  CMD=$1
  shift;
  ARGS=$@
  WIN_PWD=`wslpath -w "$(pwd)"`
  cmd.exe /c "pushd ${WIN_PWD} && ${CMD} ${ARGS}"
}

then I can simply call cmd yarn in WSL terminal like it's running yarn in Windows environment.

@therealkenc
yes, I want to have the yarn/npm running in Windows-land. I'm using neovim in WSL as the code editor, but running electron in windows as the runtime for my code. Running yarn/npm in Windows-land makes it download pre-built electron binary for Windows but Linux.

@aartek
Copy link

aartek commented Sep 16, 2022

@oHaiyang's fish function equivalent to run windows scripts from fish on wsl

function cmd 
    set CMD $argv[1]
    set ARGS $argv[2..-1]
    set WIN_PWD (wslpath -w (pwd))
    echo $WIN_PWD
    cmd.exe /c "pushd $WIN_PWD && $CMD $ARGS"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants