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
I needed to test for macOS
in addition to Windows Subsystem for Linux 2
.
This is the simplest thing working for us.
if [[ $OSTYPE == darwin* ]]; then
# macOS
elif [[ "$(</proc/sys/kernel/osrelease)" == *microsoft* ]]; then
# WSL2
else
# Other *nix distro.
fi
NOTE: The if
order matters. On macOS you get this error when looking at proc/version
.
/proc/version: No such file or directory
hat-tip @Niklas Holm and @Marc Cornellà in the top answer's comments for aiming me toward the correct WSL check.
Updating answer by @per-lundberg:
if [[ ! -z "$IS_WSL" && ! -z "$WSL_DISTRO_NAME" ]]; then
echo "You are not in wsl!"
else
echo "You are in wsl!"
fi
Note: IS_WSL
existed in older verion while WSL_DISTRO_NAME
exist in current version.
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.
Here's what I put in my .bashrc
if [[ $(uname -v | sed -rE 's/^#[0-9]{3,}-(\S+).+/\1/') == "Microsoft" ]]; then
# WSL-specific code
fi
uname -v
gets the kernel version in the format of #379-Microsoft Wed Mar 06 19:16:00 PST 2019
and the sed expression pulls out the Microsoft
string.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.)
I've been looking for ways to detect that as well. So far I've found 2.
/proc/sys/kernel/osrelease
is "3.4.0-Microsoft"
/proc/version
is "Linux version 3.4.0-Microsoft
(Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT
Wed Dec 31 14:42:53 PST 2014"
If you just use the Ubuntu distribution installed by default there should be no problems with using them, as they said that it would be unlikely for them to set either to something that doesn't contain "Microsoft" or "WSL".
However, if you were to install a different Linux distribution, I'm pretty sure that the contents of /proc/sys/kernel/osrelease
and /proc/version
will change, since the distro wouldn't have been compiled by Microsoft.