Is it possible to query the current user's Apple ID from the command line?

Solution 1:

The currently logged in Apple IDs for iCloud are stored in the MobileMeAccounts preferences. You can query the entire store with:

defaults read MobileMeAccounts Accounts

To return just the first account ID (an email address nowadays):

/usr/libexec/PlistBuddy -c "print :Accounts:0:AccountID" ~/Library/Preferences/MobileMeAccounts.plist