Shell script for turning your macOS laptop's Wi-Fi off and on

Saturday, July 6, 2024 at 5:32 PM | 5 min read

Last modified on Friday, August 7, 2026 at 10:29 AM

, , , , , , ,

Image of a wooden gate with the words "There is no Wi-Fi in the forest but you will find a better connection"

Photo by Ann on unsplash.com

Table of Contents

Update August 7, 2026: There has been much debate as to the status of AppleScript. "Is it in maintenance mode?", "Are Apple Shortcuts replacing AppleScript?", and so on. I had quite a few AppleScripts which no longer work, so something has definitely shifted in the AppleScript ecosystem. I wrote this article at a time when my AppleScripts were still working. Consider any reference to AppleScripts or articles on AppleScripts as outdated.

Creating a shell script file

Recently I wrote a couple of articles regarding AppleScript and an article regarding integrating an AppleScript into a zsh shell script. Now I'd like to talk about creating a shell script in macOS that turns off and on the WiFi on my macOS laptop.

First I create a wifi_reset.sh file inside a folder called macos-scripting located at the root of my home folder (~):

~/ #home folder macos-scripting/ concatenate.sh concatenate.txt download_install_firefox.sh play_album.sh play_song.sh wifi_reset.sh

Adding the shebang and path to the zsh binary to the file

Next, I add the shebang and path to zsh binary at the top of the file:

#!/bin/zsh

Adding comments to the shell script

Then I add a set of comments that is sometimes referred to as a script frame or set of commented steps that helps me better think through the code I need to create in each step of the scripting process:

# Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi # wait for a few seconds # turn on Wi-Fi

Adding code for turning off Wi-Fi

First let's add the code for turning off the Wi-Fi and break it down:

# Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi networksetup -setairportpower en0 off # wait for a few seconds # turn on Wi-Fi

networksetup is a macOS built-in tool that controls and modifies network settings from the command line (Terminal). Specifically, it is a configuration tool for controlling network settings located in System Preferences.

The -setairportpower flag sets Wi-Fi power to either on or off. To learn more about networksetup and its available flags, you can run man networksetup in Terminal.

en0 represents my laptop's hardware port. It follows the -setairportpower flag. However, I need to take another step via Terminal to find out what the value of my Wi-Fi's hardware port is. The syntax for the -setairportpower flag is:

-setairportpower hardwareport on | off

Finding out the hardware port of a device's Wi-Fi

In order to find out what the hardware port of my laptop's Wi-Fi is, I run the following command in Terminal:

networksetup -listallhardwareports

And it yields:

# The ethernet addresses have been removed Hardware Port: Ethernet Adapter (en4) Device: en4 Ethernet Address: Hardware Port: Ethernet Adapter (en5) Device: en5 Ethernet Address: Hardware Port: Ethernet Adapter (en6) Device: en6 Ethernet Address: Hardware Port: Thunderbolt Bridge Device: bridge0 Ethernet Address: Hardware Port: Wi-Fi Device: en0 Ethernet Address: Hardware Port: Thunderbolt 1 Device: en1 Ethernet Address: Hardware Port: Thunderbolt 2 Device: en2 Ethernet Address: Hardware Port: Thunderbolt 3 Device: en3 Ethernet Address: VLAN Configurations ===================

For the Hardware Port: Wi-Fi entry, the value of its Device key is en0. So that is what follows networksetup -setairportpower. And since I want to turn off my Wi-Fi, off follows en0.

Adding the code to turn on the Wi-Fi again

Next, I add the code for turning on my Wi-Fi again:

# Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi networksetup -setairportpower en0 off # wait for a few seconds # turn on Wi-Fi networksetup -setairportpower en0 on

I also want to set a delay between turning Wi-Fi off and on again, so that switching it off is actually perceptible:

# Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi networksetup -setairportpower en0 off # wait for a few seconds sleep 5 # turn on Wi-Fi networksetup -setairportpower en0 on

The macOS sleep command takes a single argument, which is the length of time that you want to pause before restarting a process such as turning on the Wi-Fi again. Here, the length of the pause is five seconds.

Using the echo command to add feedback in Terminal while the shell script is running

I can also add feedback in the Terminal while the shell script is running so that I better know what is going on. When is the Wi-Fi turned off and when is it turned on again? So I add the following:

# Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi echo "Disabling Wi-Fi" networksetup -setairportpower en0 off # wait for a few seconds sleep 5 # turn on Wi-Fi networksetup -setairportpower en0 on

The complete script

The complete wifi_reset.sh script:

#!/bin/zsh # Reset Wi-Fi # by turning it off and on again # turn off Wi-Fi echo "Disabling Wi-Fi" networksetup -setairportpower en0 off # wait for five seconds echo "Waiting..." sleep 5 # turn on Wi-Fi networksetup -setairportpower en0 on echo "Re-enabled Wi-Fi"

Running the script

Now, when I run the ./wifi_reset.sh shell script, the following is returned in Terminal:

Disabling Wi-Fi Waiting... Re-enabled Wi-Fi

If I removed the echo commands, nothing would be output to Terminal. I would just have to watch the Wi-Fi status menu icon in the Apple menu to see when the icon transforms into a \ (off) and then the Wi-Fi icon again (on).

Note: the echo command outputs text that follows the command. And if you want to output text which consists of multiple lines, you would wrap it in double quotes.

Changing the shell script's file permissions

Last but not least, I have to change my shell script file's permissions. If I did not complete this step, by default, I would not be able to run the script:

ls -l wifi_reset.sh -rw-r--r-- 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

-rw- represents the permissions of the owner of the file (me). The first - indicates a file, and rw- represents the default permissions of the owner of the file. The owner has read (r) and write permissions, but not execute (x) permissions. So if I try and execute the wifi_reset.sh without changing the default permissions, the following is returned in Terminal:

zsh: permission denied: ./wifi_reset.sh

If I run the following command to change the shell script's file permissions within the macos-scripting folder in Terminal (where the shell script resides):

chmod +x wifi_reset.sh

And then run the ls -l wifi_reset.sh command, it returns:

-rwxr-xr-x 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

chmod +x automatically adds execute permissions to the owner, groups, and other (users) on macOS. Even though I am the only user on my computer, maybe I still want to be extra careful and only add execute permissions to myself! In that case, I would run the following chmod command instead:

chmod 700 wifi_reset.sh

And when I run the ls -l wifi_reset.sh command, it returns the following in Terminal:

-rwx------ 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

Now groups and other (users) don't have the default read (r) anymore. I set all my permissions this way. It's much safer!

Note: the chmod, aka change mode command modifies file and directory access permissions. It controls whether the file owner, a designated user group, or everyone else can read, write, or execute a file.

Conclusion

In this post, I walk through how to create a shell script to turn my macOS laptop's Wi-Fi off and on. I introduce the macOS networksetup tool that controls network settings from the command line. And I also include the networksetup flag, -setairportpower, that turns a device's Wi-Fi on and off. And last but not least, is en0, that is the primary Ethernet or Wi-Fi interface on macOS, and it represents the main network connection used for internet access. The shell script file setup itself applies to any shell script. For those of you new to the concept of networksetup or shell scripting, or both, this is a good and useful place to begin!

loading