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

后端 未结 10 653
时光说笑
时光说笑 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:39

    For WSL2, we can no longer detect through kernel version because it is running an actual Linux kernel in Hyper-V. However, it still can call explorer.exe existing in every Windows installation. So we could...

    if [ -x "$(command -v explorer.exe)" ]; then
    echo "We are running on WSL"
    fi
    

    This should be a more generic way to detect if the script is running on WSL.

    Edit: See answers above. I forgot to count Unix-like environments like Msys2 in.

提交回复
热议问题