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 identical to a native GNU/Linux install and /etc/os-version
is identical to a Ubuntu Trusty Tahr install.
很简单,通常的位置来计算你所使用的操作系统在Ubuntu上看起来和Ubuntu的Windows是一样的。例如,uname -a与本地的GNU/Linux安装和/etc/os版本相同,它与Ubuntu可靠的Tahr安装完全相同。
The only thing I can think of is to check if /mnt/c/Windows
exists, but I'm not sure if that's a foolproof idea.
我能想到的唯一一件事就是检查/mnt/c/Windows是否存在,但我不确定这是不是一个万无一失的想法。
3 个解决方案
#1
15
The following works in bash on Windows 10, macOS, and Linux:
bash适用于Windows 10、macOS和Linux:
#!/bin/bash
set -e
if grep -qE "(Microsoft|WSL)" /proc/version &> /dev/null ; then
echo "Windows 10 Bash"
else
echo "Anything else"
fi
You need to check for both "Microsoft" and "WSL" per this comment by Ben Hillis, WSL Developer:
您需要根据WSL开发人员Ben Hillis的评论检查“Microsoft”和“WSL”:
For the time being this is probably the best way to do it. I can't promise that we'll never change the content of these ProcFs files, but I think it's unlikely we'll change it to something that doesn't contain "Microsoft" or "WSL".
目前,这可能是最好的方法。我不能保证我们永远不会改变ProcFs文件的内容,但是我认为我们不太可能把它变成不包含“Microsoft”或“WSL”的文件。
/proc/sys/kernel/osrelease /proc/version
#2
5
I've been looking for ways to detect that as well. So far I've found 2.
我也一直在寻找检测的方法。到目前为止,我已经找到了2个。
-
/proc/sys/kernel/osrelease
is "3.4.0-Microsoft"/proc/sys/kernel/osrelease“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"/proc/version是“Linux 3.0 .0- microsoft (Microsoft@Microsoft.com) (gcc 4.7版本)”
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".
如果您只是使用默认安装的Ubuntu发行版,那么使用它们应该没有问题,因为他们说不太可能将它们设置为不包含“Microsoft”或“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.
但是,如果您要安装一个不同的Linux发行版,我敢肯定/proc/sys/kernel/osrelease和/proc/version的内容将会改变,因为发行版不是由微软编译的。
#3
1
I just came up with this for my .bashrc for adding some WSL items to $PATH.
我刚刚为我的.bashrc提供了这个,用于将一些WSL项目添加到$PATH中。
Works in 1703. Not sure if earlier versions.
在1703年的作品。不确定是否早期版本。
if [[ $(uname -r) =~ Microsoft$ ]]; then
foo
fi
#1
15
The following works in bash on Windows 10, macOS, and Linux:
bash适用于Windows 10、macOS和Linux:
#!/bin/bash
set -e
if grep -qE "(Microsoft|WSL)" /proc/version &> /dev/null ; then
echo "Windows 10 Bash"
else
echo "Anything else"
fi
You need to check for both "Microsoft" and "WSL" per this comment by Ben Hillis, WSL Developer:
您需要根据WSL开发人员Ben Hillis的评论检查“Microsoft”和“WSL”:
For the time being this is probably the best way to do it. I can't promise that we'll never change the content of these ProcFs files, but I think it's unlikely we'll change it to something that doesn't contain "Microsoft" or "WSL".
目前,这可能是最好的方法。我不能保证我们永远不会改变ProcFs文件的内容,但是我认为我们不太可能把它变成不包含“Microsoft”或“WSL”的文件。
/proc/sys/kernel/osrelease /proc/version
#2
5
I've been looking for ways to detect that as well. So far I've found 2.
我也一直在寻找检测的方法。到目前为止,我已经找到了2个。
-
/proc/sys/kernel/osrelease
is "3.4.0-Microsoft"/proc/sys/kernel/osrelease“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"/proc/version是“Linux 3.0 .0- microsoft (Microsoft@Microsoft.com) (gcc 4.7版本)”
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".
如果您只是使用默认安装的Ubuntu发行版,那么使用它们应该没有问题,因为他们说不太可能将它们设置为不包含“Microsoft”或“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.
但是,如果您要安装一个不同的Linux发行版,我敢肯定/proc/sys/kernel/osrelease和/proc/version的内容将会改变,因为发行版不是由微软编译的。
#3
1
I just came up with this for my .bashrc for adding some WSL items to $PATH.
我刚刚为我的.bashrc提供了这个,用于将一些WSL项目添加到$PATH中。
Works in 1703. Not sure if earlier versions.
在1703年的作品。不确定是否早期版本。
if [[ $(uname -r) =~ Microsoft$ ]]; then
foo
fi