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:

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