I don’t think a solution purely in bash would be possible, since we are trying to interface with internal macOS application events. Further, I could not find direct reference to AirPlay, Displays, or Mirroring within the Script Editor.app’s Dictionary (open Script Editor.app, File → Open Dictionary…). But this still leaves the option of GUI scripting via AppleScript, which I will offer below.
In macOS 11+, this old example syntax exemplified here won’t work because of the UI changes that have been made since macOS 11. It may be possible to modify it for the new Control Center Menu Bar item, as was done successfully for a checkbox selection type in the second example here, but I haven’t been able to adapt it successfully for Screen Mirroring. Others have also been attempting this.
In the meantime, it can still be accomplished with some AppleScript GUI scripting via the System Preferences route, as apposed to going through the system Menu Bar’s Control Center:
tell application "System Preferences" quit end tell tell application "System Preferences" activate set current pane to pane "com.apple.preference.displays" end tell tell application "System Events" tell application process "System Preferences" repeat until exists window "Displays" end repeat tell pop up button 1 of window 1 click click menu item "Apple TV" of menu 1 delay 1 end tell end tell end tell tell application "System Preferences" quit end tell
Be sure to hardcode the name of your device in place of “Apple TV”. Further, you could pass an argument to toggle AirPlay mirroring for devices on the fly with a proper Automator Automation, or if you wrap this script into a shell script or a bash alias.