Ok I need to determine the system's OS from a Lua script, but Lua as such has no API for this, so I use os.getenv() and query enviromental variables. On Windows checking the enviromental variable "OS" gives me the name of the system's OS, but is there some variable that exists on both Windows and most flavors of Unix that can be checked?
6 Answers
You can try package.config:sub(1,1)
. It returns the path separator, which is '\\'
on Windows and '/'
on Unixes...

- 1,308
- 11
- 24
-
1this is a simple and clever answer – tcpiper Sep 01 '16 at 09:43
-
2just to back this up: [Lua 5.3 reference](http://www.lua.org/manual/5.3/manual.html#pdf-package.config) – Chris K Jun 18 '17 at 14:06
-
3With Lua 5.3 on Win10, print(package.config:sub(1,1)) yields in '\' and not '\\' as claimed by mnicky in 2013. But great idea to differ Win from Linux :-), anyhow. – Rolf Hemmerling - user3283760 Jun 07 '21 at 19:45
-
The first backslash in `'\\'` is an escape character, needed for hard-coding stuff like `if package.config:sub(1,1) == '\\' then`, the raw string is indeed just ``\`` – AgentRev Jun 04 '23 at 14:11
On a Unix system, try os.capture 'uname' where os.capture is defined below:
function os.capture(cmd, raw) local f = assert(io.popen(cmd, 'r')) local s = assert(f:read('*a')) f:close() if raw then return s end s = string.gsub(s, '^%s+', '') s = string.gsub(s, '%s+$', '') s = string.gsub(s, '[\n\r]+', ' ') return s end
This will help on all flavors of unix and on Mac OSX. If it fails, you might be on a Windows system? Or check os.getenv 'HOME'.

- 198,648
- 61
- 360
- 533
When lua is compiled, it is configured slightly differently depending on what operating system it is compiled for.
Many of the strings which are set in the 'package' module can thus be used to distinguish which system it was compiled for.
For instance, when lua loads C-based modules which are distributed as dynamic libraries, it has to know the extension used for those libraries, which is different on each OS.
Thus, you can use a function like the following to determine the OS.
local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
if BinaryFormat == "dll" then
function os.name()
return "Windows"
end
elseif BinaryFormat == "so" then
function os.name()
return "Linux"
end
elseif BinaryFormat == "dylib" then
function os.name()
return "MacOS"
end
end
BinaryFormat = nil

- 15,614
- 4
- 51
- 87

- 71
- 1
- 1
-
I'm sorry for posting this that way, I'm not used to stack overflow. – Matías Hermosilla Jun 21 '15 at 00:07
-
5Not quite reliable in all cases. I have a C program that embeds the lua interpreter and the lua code is returning Linux as the BinaryFormat on OSX. – hookenz Dec 02 '15 at 19:40
-
I guess that if you just need Windows/Unix detection, you could check the filesystem for the existence of /etc or /bin or /boot directories. Aditionally, if you need to know which distro is it, most Linux distros have a little file in /etc showing the distro and version, sadly they all name it differently.

- 330,807
- 53
- 334
- 373
-
Detecting folders such as `/Applications` for MAC OS, `/home` (after trying MAC OS) for GNU/Linux and finaly `C:/Windows` and `C:/WINNT` is a good solution for me. – MARTIN Damien Jan 20 '13 at 09:06
FWIW, the simplest function I can think of:
function MyScript:OS()
return package.config:sub(1,1) == "\\" and "win" or "unix"
end

- 314
- 1
- 2
- 9
Unixes should have the $HOME variable (while Windows doesn't have that), so you can check it (after checking the OS variable is empty).

- 6,996
- 4
- 44
- 81
-
1On each windows version I've checked, there is no HOME environment variable. Try to do "echo %HOME%" from the command prompt. – friol Oct 21 '12 at 08:09
-
Windows 7(v 6.1, build 7601) has HOME, what OS version did you check? – Baiyan Huang Oct 21 '12 at 08:15
-
-
-
1My win7 pro 64-bit has `HOMEPATH` and `HOMEDRIVE` but no `HOME`. I don't have Cygwin. IMHO, checking the environment is going to be fragile since any user might have something like Cygwin present or have decided that `HOME` is convenient. Also, when running under Cygwin, is the system really Windows any more? This quickly becomes a thorny question. – RBerteig Apr 24 '13 at 17:37
-
I just spent an hour trying to debug a Lua script that uses the `$HOME` method of detecting the OS. For some reason my Windows environment got that set and chaos ensued. Find another way (the `package.config:sub(1,1)` mentioned above works well). – Perry May 16 '14 at 23:33
-
windows 10 and onwards uses powershell as the default shell, and thus has always had `$HOME` mean the same thing as it does on linux and mac. – airtonix Dec 26 '22 at 10:01