In short, I’m writing a script that will check if a computer is a MacBook Pro or a MacBook Air and then I want to use the output as a variable.


system_profiler SPHardwareDataType | grep “Model Name”

I can get Model Name: MacBook Pro but all I need is either “MacBook Pro” or “MacBook Air”

-oP in grep gives me an error. Another idea was to use find but I’m unsure how I’d use find if I’m not searching an actual file.


Here's a solution using awk with system_profiler :

 system_profiler SPHardwareDataType | awk -F ': ' '/Model Name/ { print $2 }'

We change the field separator to : <space> then search for a line with Model Name in it and print the second field. Or instead of using a pipe use process substitution to feed awk

awk -F ': ' '/Model Name/ { print $2 }' < <(system_profiler SPHardwareDataType)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .