This is not your average post about how to get the serial number for your Mac from the command line or a script. Instead of showing how to do it, I’ll examine a few ways you might do it with via the provided UNIX shells, and compare how quickly they run.
There are two major ways I’m aware of to get the serial number of a Mac from the shell: the ioreg and system_profiler utilities. The output of either generally needs finessing to be useful — at the very least, you’ll want to narrow down the results to just the serial number if that’s all the data you need.
(However, if you need more data, don’t prematurely optimize for speed. Calling either tool multiple times without caching the results would likely result in a performance penalty. I would suspect this would get worse if you're calling one of the shell tools from within a Python or Perl script.)
Note that both shell tools require that your motherboard have a serial number assigned to it in order to show it to you. (You may not have this on a particular Mac if its motherboard has been repaired by replacement and your Apple Authorized Service Provider did not flash it with the serial number.) If you can’t see a Mac’s serial number in the “About this Mac” dialog box, you shouldn’t be able to read it with these other tools, either.
Let's start with the ioreg utility.
$ ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'
W55512LPW3K
I realize you could get away without the printf statement but I kind-of like to make sure my output is formatted.
You could do the following, but it doesn’t return the raw serial number. Instead, the results are returned surrounded by quotes. (This is what I got off the MacEnterprise list, in a post by Dennis Lorenzen.
$ ioreg -l | awk '/IOPlatformSerialNumber/ { print $4; }'
"W55512LPW3K"
I found a negligible speed difference between various versions when I ran them with time, so stripping the quotes and using printf appeared to be reasonable. The extra processing arguably returns better output.
Most of the work appears to happen in ioreg. For example:
$ time ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'
W55512LPW3K
ioreg -l 0.10s user 0.14s system 97% cpu 0.252 total
awk 0.01s user 0.00s system 5% cpu 0.250 total
You can get the same result with similar performance via system_profiler. I actually didn't expect that it would compare favorably, but it does. Here, I'll use another example posted in that MacEnterprise thread:
$ time system_profiler SPHardwareDataType | grep "Serial Number" | awk '{ print $3; }'
W55512LPW3K
system_profiler SPHardwareDataType 0.04s user 0.04s system 29% cpu 0.263 total
grep "Serial Number" 0.00s user 0.00s system 1% cpu 0.262 total
awk '{ print $3; }' 0.00s user 0.00s system 1% cpu 0.260 total
But if you optimize a bit to eliminate the grep step — because it is superfluous if you're already calling awk — you do get the fastest results I’ve come across. It's worth noting that they have been variable results, especially but not always between the first time and subsequent runs. So, I wouldn't be surprised if there was some other caching or other mechanism at work that helps make speed this method along.
$ time system_profiler SPHardwareDataType | awk '/Serial Number/ { print $3; }'
W55512LPW3K
system_profiler SPHardwareDataType 0.04s user 0.04s system 51% cpu 0.156 total
awk '/Serial Number/ { print $3; }' 0.00s user 0.00s system 2% cpu 0.155 total
Credit goes to the discussion of this topic on the MacEnterprise mailing list, but if you Google for the topic you’ll find a lot of hits and helpful information. I did a little bit of extra editing in awk to strip the quote marks and time the execution for the ioreg version. I’m also happy I did the timing so that I know that the system_profiler route does appear to be the quickest in overall execution and low in CPU usage.