Bash on Windows - alias for exe files

不问归期 提交于 2019-12-01 06:19:15

You should be able to simply set the executable directory to your PATH. Use export to persist.

Command: export PATH=$PATH:/path/to/directory/executable/is/located/in

I've faced the same problem when trying to use Docker for Windows from WSL.

Had plenty of existing shell scripts that run fine under Linux and mostly under WSL too until failing due to docker: command not found. Changing everywhere docker to docker.exe would be too cumbersome and non-portable.


Tried workaround with aliases in ~/.bashrc as here at first:

shopt -s expand_aliases
alias docker=docker.exe
alias docker-compose=docker-compose.exe

But it requires every script to be run in interactive mode and still doesn't work within backticks without script modification.


Then tried exported bash functions in ~/.bashrc:

docker() { docker.exe "$@"; }
export -f docker
docker-compose() { docker-compose.exe "$@"; }
export -f docker-compose

This works. But it's still too tedious to add every needed exe.


Finally ended up with easier symlinks approach and a modified wslshim custom helper script.

Just add once to ~/.local/bin/wslshim:

#!/bin/bash -x
cd ~/.local/bin && ln -s "`which $1.exe`" "$1" || ln -s "`which $1.ps1`" "$1" || ln -s "`which $1.cmd`" "$1" || ln -s "`which $1.bat`" "$1"

Make it executable: chmod +x ~/.local/bin/wslshim

Then adding any "alias" becomes as easy as typing two words:

$ wslshim docker
+ cd ~/.local/bin
++ which docker.exe
+ ln -s '/mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe' docker

$ wslshim winrm
+ cd ~/.local/bin
++ which winrm.exe
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.ps1
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.cmd
+ ln -s /mnt/c/Windows/System32/winrm.cmd winrm

The script auto picks up an absolute path to any windows executable in $PATH and symlinks it without extension into ~/.local/bin which also resides in $PATH on WSL.

This approach can be easily extended further to auto link any exe in a given directory if needed. But linking the whole $PATH would be an overkill. )

In my windows 10 the solution was to install git-bash and docker for windows.

in this bash, when I press "docker" it works for example "docker ps" I didnt need to make an alias or change the path.

you can download git-bash from https://git-scm.com/download/win

then from Start button, search "git bash". Hope this solution good for you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!