How to check if a program is run in Bash on Ubuntu on Windows and not just plain Ubuntu?

后端 未结 10 607
时光说笑
时光说笑 2020-12-05 02:07

Pretty straightforward, the usual places to figure out the OS you\'re on seem to be identical to plain Ubuntu on Ubuntu for Windows. For example uname -a is ide

10条回答
  •  时光取名叫无心
    2020-12-05 02:42

    Without me doing anything special, these environment variables seem to be set already:

    $ set | grep WSL
    IS_WSL='Linux version 4.4.0-18362-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #1-Microsoft Mon Mar 18 12:02:00 PST 2019'
    WSLENV=
    WSL_DISTRO_NAME=Debian
    

    So, something like the following snippet should also work in this case (example of what I used it for myself):

    if [ ! -z "$IS_WSL" ]; then
        alias code='/mnt/c/Users/per/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe'
    fi
    

    (Note that technically, -z does not check if the variable is unset, merely that it is empty; in practice, this works well enough in this case. The ! at the beginning is there to negate the check.)

提交回复
热议问题