0

I was hoping somebody could explain bit by bit what each part of this command does.

ifconfig | grep -Po "HWaddr \K.*$"

e.g. what The -Po is for?

  • 2
    What research have you done so far on this? – bertieb Feb 16 '17 at 14:17
  • 1
    I found that -P is short for --perl-regexp and means "Interpret the pattern as a Perl-compatible regular expression (PCRE).". -o is short for --only-matching and means "Print only the matched (non-empty) parts of matching lines...". – IQV Feb 16 '17 at 14:19
5

What does this command do?

ifconfig | grep -Po "HWaddr \K.*$"

prints out the MAC/hardware addresses of active network interfaces present on your system, if available.


As an aside, in Linux in general you can find out about programs and what their switches do by looking them up in man, eg:

$ man ifconfig

$ man grep

(etc) Failing that, a search for '<program name> manual' or '<program name> usage' can answer these sorts of questions.

ifconfig

ifconfig is used to display and configure network interfaces. From man ifconfig:

Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single interface argument is given, it displays the status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures an interface.

(emphasis mine)

Here, ifconfig is reporting the status of your currently active interfaces.

| (pipe)

The pipe character - | - here redirects the output from ifconfig and instead of it being displayed to you, it instead goes to grep

grep

grep is used for pattern matching, and (usually) printing what matches a pattern or not.

Here, the options supplied are -P and -o

-P:

Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented features.

Regexes are a long topic1, but basically, this lets grep match some additional patterns.

-o:

Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

(both quotes from grep manpage)

This option tells grep that we're only interested in the part that matches the pattern, as opposed to the whole line (if it matches).

"HWaddr \K.*$"

This part is what tells us we are looking for the HWaddr (MAC or hardware address). \K sets the start point of what we want printed (for the -o option above). .* tells grep to match any character (.) as many times as possible (*), at least until the end of the line ($).

Output / Result

Putting it all together, the command takes information from ifconfig and searches for the parts matching the MAC address of network interfaces on the system, and prints those out each on a new line.


1: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems." Jamie Zawinski, 1997 (although the quote is often taken out of context)

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.