How to detect the OS from a Bash script?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Detecting the operating system in a Bash script usually starts with uname, but the right level of detail depends on what you actually need. Sometimes you only need to distinguish Linux from macOS, and sometimes you need distribution-specific information such as Ubuntu versus Fedora.
The Most Portable First Check: uname
For broad operating-system detection, uname -s is the usual starting point.
Typical outputs include:
- '
Linux' - '
Darwinfor macOS' - '
FreeBSD'
That is often enough when the script only needs coarse branching.
A Simple case Statement
A case statement keeps the logic readable.
This is a good default if the script needs only major OS families.
Linux Distribution Detection
If you need more detail on Linux, /etc/os-release is usually the best source.
This is much better than guessing distribution based only on uname, because uname -s will usually just say Linux regardless of distro.
Combining OS and Distribution Logic
A practical script often does both:
This gives you a clean branch for Linux family and then refines the result with distro metadata.
What About WSL or Similar Environments?
Sometimes the real question is not only "Linux or macOS?" but whether the script is running under a compatibility layer such as WSL.
A simple heuristic is:
That is environment-specific, so it should be treated as a secondary check after your normal OS detection.
Keep the Goal in Mind
Do not collect more OS detail than you need. For many scripts, all that matters is one capability difference, such as whether sed -i behaves like GNU sed or BSD sed.
In those cases, sometimes feature detection is better than OS detection. For example, it can be better to test whether a command-line option exists than to infer support indirectly from the platform name.
A Practical Example
Here is a compact script that handles common Linux and macOS branching.
That is enough for many installation scripts and local tooling wrappers.
Common Pitfalls
The biggest mistake is assuming uname -s tells you the Linux distribution. It does not; it usually only says Linux.
Another issue is parsing random release files with distro-specific assumptions when /etc/os-release would have been the cleaner source.
Developers also sometimes overuse OS detection when feature detection would be more robust. If the real requirement is command behavior, test the command capability directly when possible.
Finally, avoid unquoted variable expansions in shell conditionals. OS-detection code should be boring and reliable.
Summary
- Use
uname -sfor broad operating-system detection. - Use
/etc/os-releasewhen you need Linux distribution details. - Combine the two checks when necessary.
- Prefer feature detection over OS detection when the script only cares about behavior.
- Keep the branching logic small and explicit so the script remains portable.

