I'm trying to automatically pull out the mac address of a arduino device using a shell script, and need some help how to do this.

This is the output returned by the command:

Opening /dev/cu.SLAB_USBtoUART @ 115200...
Connecting to ESP32 ROM, attempt 1 of 10...
Connecting to ESP32 ROM, attempt 2 of 10...
  Connected, chip: ESP32D0WDQ5 R1
efuse_wr_disable     : 0x0084
efuse_rd_disable     : 0x1
flash_crypt_cnt      : 0x01 (WD)
WIFI_MAC_Address     : 0xaaaaaaaaaaaaaa (MAC: aa:bb:cc:dd:ee:ff)
SPI_pad_config_hd    : 0x0
chip_package         : 0x1
cpu_freq_low         : 0x0

I am able to capture this into a shell script variable, but having a hard time trying to extract just the mac address of aa:bb:cc:dd:ee:ff

The output will always be the exact same, (MAC: followed by the 17 characters that make up the mac address, then a closing )

Can someone please help me with a command I can use on a variable all this output is stored in, to get just the aa:bb:cc:dd:ee:ff portion? I've been searching and trying for hours without luck, thank you!!

3 Answers 3


You can try awk

my_command_with_output | awk -F'[()]' '/^WIFI_MAC_Address/{sub(/^.*: /,"");print $1}'

You can put it in a varialbe

variable=$(my_command_with_output | awk -F'[()]' '/^WIFI_MAC_Address/{sub(/^.*: /,"");print $1}')

Print out the content of the variable.

echo "$variable"


  • 1
    awesome, this works great thank you!!
    – sMyles
    Mar 26, 2020 at 23:56
  • Great, good luck with your script. :-)
    – Jetchisel
    Mar 26, 2020 at 23:58
  • would you be able to help with the awk cmd to get the flash_crypt_cnt value as well? Will always be the same format, and value will either be 0x0 or 0x1 thanks!
    – sMyles
    Mar 27, 2020 at 0:16
  • awk -F'[(:) ]+' '/^flash_crypt_cnt/{print $2}'
    – Jetchisel
    Mar 27, 2020 at 0:23

If I understand you correctly:

echo "$var" |  grep -o "..:..:..:..:.."


  • Oh wow this was so simple how did i not come across this during my search, works great thank you!
    – sMyles
    Mar 26, 2020 at 23:56

You can capture any valid MAC (with a leading space) with this regex:

command  |   grep -oE '([ :][[:xdigit:]]{2}){6}'

Or, in bash shell:

$ regex='(([[:xdigit:]]{2}:){5}([[:xdigit:]]{2}))';
$ [[ $var =~ $regex ]] &&  printf '%s\n' "${BASH_REMATCH[1]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.