Here’s my first (admittedly ugly) attempt:

system_profiler SPHardwareDataType | grep 'Serial' | sed 's/^.*\:\ //'

Here’s a better one from stack exchange:

system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'

Check your warranty status:

Update: Within a year or two of this post, Apple disabled direct URL links to hardware warranty checks, so the following no-longer works.

open "https://selfsolve.apple.com/agreementWarrantyDynamic.do?sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')&locale=en_US&caller=sp"