Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell apart Linux from Windows 10 bash while making bash scripts

I'm making a bash script. My custom bashrc, to be more precise. And I need to have separate configs, depending on what "Linux" I am in (the Windows 10 Linux subsystem or actual Linux).

How can I tell them apart?

Things I tried:

  1. $OSTYPE - both return linux-gnu
  2. uname -a - both return similar Linux COMPUTER_NAME_HERE 3.4.0+ #1 PREEMPT Thu Aug 1 17:06:05 CST 2013 x86_64 x86_64 x86_64 GNU/Linux
  3. lsb_release -a - both return ubuntu xx.xx
  4. cat /etc/issue.net - both return Ubuntu 14.04.4 LTS
  5. ld -v - both return GNU ld (GNU Binutils for Ubuntu) 2.24

The only way I have found to make this work is to set some specific username for the Windows sub-system, and use $USER, but that is not an option I'm willing to use. I need this to work out of the box, without custom user names, environment variables, etc..

like image 348
PragmaticEd Avatar asked Aug 05 '16 05:08

PragmaticEd


1 Answers

See /proc/sys/kernel/osrelease

You can check for Windows with grep -qi Microsoft /proc/sys/kernel/osrelease 2> /dev/null

like image 58
laktak Avatar answered Nov 02 '22 06:11

laktak