macOS Monterey or earlier: Use System Preferences โ Apple ID.
defaults read MobileMeAccounts
dscl . -read /Users/$(whoami) AuthenticationAuthority | grep -i icloud
system_profiler SPAppleIDDataType
Example output when signed in:
Apple ID:
Name: John Doe
Email: john.doe@icloud.com
If not signed in:
No Apple account is currently signed in.
#!/bin/bash
if system_profiler SPAppleIDDataType | grep -q "No Apple account"; then
echo "Not signed in with Apple ID."
else
echo "Apple ID is active:"
system_profiler SPAppleIDDataType | grep -E "Name|Email"
fi