Monday, September 23, 2013

Toggle a MacBook's Wifi with a Bash One-Liner

Sometimes the easiest way is a bash one-liner.  (Think Alfred shortcuts, BetterTouchTool gestures, Terminal.app.)  Running an applescript is all fine and dandy, but it's heavy.  Here's how to toggle your MacBook wifi on or off, where the wifi interface is en0.

( networksetup -getairportpower en0 | grep On ) \
   && ( networksetup -setairportpower en0 Off ) \
   || ( networksetup -setairportpower en0 On )

This is one of those messed-up bash-isms that uses conditional logic in the "wrong" way, elegantly.

No comments:

Post a Comment