Is there any reliable way to determine (programmatically) from within a bash script if it is being executed on a laptop or a desktop computer?

I could obviously just prompt the user to ask them, but that is pretty clunky.

  • 1
    I'd say why you need to know? Are you worried about battery life – Rich Homolka Feb 17 '15 at 22:29
  • 3
    The script is doing some system setup for the user, and part of that is configuring some power saving settings if the device is a laptop. – PseudoPsyche Feb 17 '15 at 23:18

Looking at whether or not the system has a battery is not reliable - a UPS connected to the system (not just for power, but over USB as well for automatic shutdown and battery monitoring) may show up as a battery.

There is a nice reliable way however:

dmidecode --string chassis-type

On a laptop, this will return one of "Laptop", "Notebook" "Portable", or "Sub Notebook" (depending on what the manufacturer coded into the BIOS). There is a full list of possible values at "Identifying the Chassis Type of a Computer" in the Windows 2000 Scripting Guide - don't worry about it being a Microsoft TechNet page, this is not OS specific.

dmidecode can also get information about the hardware manufacturer, system serial number (sometimes), etc.

| improve this answer | |
  • I have a UPS controlled via USB for automatic shutdown and battery monitoring with Network UPS Tool. It is does not show up as a "battery" on the Desktop. What configuration are you using that under which a UPS appears in /sys/module/battery? – John1024 Feb 14 '15 at 7:35
  • This works better than the /sys/module/battery method: My two Ubuntu 14.04 desktops answer "Desktop" and "Low Profile Desktop", respectively, and the one with CentOS 6.5 says "Unknown". The one with CentOS 5.3 doesn't recognize the keyword "chassis-type". The Laptop I tried it on says "Notebook". But it's a drawback that you need root access. – Thomas Padron-McCarthy Feb 14 '15 at 9:36
  • I really like this answer! – Lembik Feb 14 '15 at 14:31
  • +1 for distribution independence. I tested it successfully on several machines. The only machine on which dmidecode gave a less than helpful answer was an Aspire netbook returned a chassis type of Other. – John1024 Feb 15 '15 at 22:07
  • this answer requires sudo, so is not viable for things like .bashrc files. Querying the contents of /sys/class/dmi/id/chassis_type, which is just a single number, is much better (as another answer below pointed out). – xdavidliu Oct 5 '19 at 20:05

To avoid using sudo, you can read the contents of /sys/class/dmi/id/chassis_type. It appears conform to the following table:

  • 1 Other
  • 2 Unknown
  • 3 Desktop
  • 4 Low Profile Desktop
  • 5 Pizza Box
  • 6 Mini Tower
  • 7 Tower
  • 8 Portable
  • 9 Laptop
  • 10 Notebook
  • 11 Hand Held
  • 12 Docking Station
  • 13 All in One
  • 14 Sub Notebook
  • 15 Space-Saving
  • 16 Lunch Box
  • 17 Main System Chassis
  • 18 Expansion Chassis
  • 19 SubChassis
  • 20 Bus Expansion Chassis
  • 21 Peripheral Chassis
  • 22 Storage Chassis
  • 23 Rack Mount Chassis
  • 24 Sealed-Case PC
| improve this answer | |
  • Source for the table? And is 'lunch box' a joke?! – OJFord Jul 14 '19 at 20:03
  • 1
    @OJFord I do not have a citation at this time. As far as 'lunchbox', it is a nickname give to a form factor. I've worked with a few lunchboxes and pizzaboxes before. While this link may not last years, you can see some very modern lunchbox computers theportablepc.com/portable-pc.html – demure Jul 14 '19 at 22:46
  • 1
    Grant's answer contains a citation from microsoft: docs.microsoft.com/en-us/previous-versions/tn-archive/… – demure Jul 14 '19 at 22:58
  • Thanks! Now I want one... But I suspect my wallet doesn't. – OJFord Jul 15 '19 at 6:04
  • This was the only solution that worked for me without needing to sudo. The battery-based solutions either said my ubuntu desktop was a laptop, or said my debian laptop was a desktop. – xdavidliu Oct 5 '19 at 19:54

Debian Solution:

To find whether a machine running Debian is a laptop, try:

[ -d /sys/module/battery ] && echo "Yes it's a laptop"

This approach does not require root privileges.

On other distributions, however, this directory seems to exist, at least in skeleton form, regardless of whether or not there is a battery. From the comments (below), these distributions include CentOS, Ubuntu, and the Ubuntu-derived distribution of Linux Mint.

More General Solution

Although it does not work on my Debian systems, the solution proposed by Alex reportedly works on Ubuntu & CentOS. Thus suggests, for greater generality, a possible combined solution:

[ -f /sys/module/battery/initstate ] || [ -d /proc/acpi/battery/BAT0 ] && echo "Yes it's a laptop"

This approach does not require root privileges.

More Details

On a Debian system with an actual battery, the /sys/module/battery directory contains many files. One such file is /sys/module/battery/initstate which contains the text live. On Ubuntu, however, these files do not exist even on actual laptops. Thus, it appears that the presence of the file /sys/module/battery/initstate can be used to test for a laptop running Debian.

On Debian systems that I tested, by contrast, the /proc/acpi/battery directory did not exist.

| improve this answer | |
  • 1
    Isn't it possible that the second command could still return true in the case of a desktop with a UPS? – PseudoPsyche Feb 13 '15 at 20:36
  • 3
    @PseudoPsyche I just tested it on a desktop with a UPS and it returned false (not a laptop). – John1024 Feb 13 '15 at 20:41
  • 1
    What is that first command doing there? – AndreKR Feb 14 '15 at 2:11
  • 1
    Apparently my 4U IBM server with an APC SmartUPS plugged in over USB is a laptop. I certainly wouldn't want that thing on my lap all day! (ie, if it has a UPS plugged in and configured for shutdown/monitoring, that directory will exist) – Grant Feb 14 '15 at 3:31
  • 1
    @Chipperyman “Doesn't a UPS go between the wall socket and a computer power socket?” Yes, and many UPS setups—including consumer level—allow you to hookup a USB cable to get battery info and alerts on your desktop. – Giacomo1968 Feb 14 '15 at 4:16

I'd check if the computer has a battery installed. And the following is one way to test:

if [ -d /proc/acpi/battery/BAT* ]; then
  echo has a battery
| improve this answer | |
  • This method gave the correct answer on all five computers I tried it on: two desktops with Ubuntu 14.04, one desktop with CentOS 5.3, one desktop with CentOS 6.5, and one laptop with Ubuntu 14.04. Also, it doesn't require root access. Of the three methods in the answers, my tests indicate that this may, tentatively, be the best one. – Thomas Padron-McCarthy Feb 14 '15 at 9:39
  • This works for me. – Lembik Feb 14 '15 at 14:38
  • This did not work on the two Debian laptops that I tested but +1 for working on a variety of other important distributions. – John1024 Feb 15 '15 at 22:01
  • Doesn't work for me (Toshiba Notebook), because my battery is named BAT1. I don't know how many other possible names there might be. – Joe Feb 17 '15 at 6:42
  • @Joe Change BAT0 to BAT* -- it should detect all variations. – Alex Feb 17 '15 at 17:24

If checking for battery existence is good enough, you can use this shell function:

# Checks whether system is a laptop.
# @returns {bool}   true if system is a laptop.
function is_laptop() {
    local d
    for d in /sys/class/power_supply /proc/acpi/battery; do
        [[ -d "$d" ]] && find $d -mindepth 1 -maxdepth 1 -name 'BAT*' -print0 -quit 2>/dev/null | grep -q . && return 0

    # note we're checking /sys/class/power_supply/battery/status for WSL
    for d in /sys/class/power_supply/battery/status /sys/module/battery/initstate; do
        [[ -f "$d" ]] && return 0

    return 1

Have been using this on Debian for years. Note this works also for Debian running in WSL or virtualbox.

Edit: generalized from other answers in this thread to catch more cases.

| improve this answer | |

Another solution that does not require sudo:

hostnamectl status | grep Chassis | cut -f2 -d ":" | tr -d ' '

Will output laptop or desktop.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.