I have a script that disables the touchpad on my thinkpad:

#!/usr/bin/env bash

xinput --disable 12

How could I adjust the script so that instead of just disabling, it will check the current state of the touchpad and toggle the enable/disable?

  • 1
    xinput list shows a list all available and attached input devices on your system.Your current device should have id=12 section in its description.You may first show the STATE section then use an case statement to toggle between enable(state 0) disabled (state 1) URL for reference askubuntu.com/questions/65951/how-to-disable-the-touchpad – FrontENG Jul 15 '17 at 0:54

It's strange that xinput can't filter its output itself. But we have grep!

xinput --list-props 12 | grep -q 'Device Enabled.*1$' && echo enabled || echo disabled


to toggle the touchpad you could run a perl one-liner from this script:

perl -e '$dev="SynPS/2 Synaptics TouchPad"; `xinput list-props $dev` =~ /^.*Device Enabled.+?(\d)$/m ;  $1 ? `xinput --disable $dev` : `xinput --enable $dev`;

short expanation:

  • the backticks execute a system/linux-command
  • while matching the line where it says "Device Enabled" the regex looks for the last numeric character on that line (which is 0 ... for off, or 1 ... for on)
  • the /m in the regex means: use each line from the output of the command as a seperate element (otherwise ^ and $ of the regex would mean the beginnen/end of the string, not the line
  • the parantheses save the matched number into the Variable $1
  • the rest is the ternary operaton: "if true" ? "do this" : "else to this", meaning if $1 equals 0 or 1 -> disable or enable

(to use this in, e.g. "i3" window manager: you would have to write:

bindsym XF86TouchpadToggle exec --no-startup-id perl -e '{`xinput list-props 10` =~ /^.*Device Enabled.+?(\d)$/m and  {$1 ? `xinput --disable 10` : `xinput --enable 10`}} '

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.