I'm trying to create a one-line, platform-independent solution for finding the local IP address of a Android device with adb shell and grep (the internal grep on my android device). I have a solution that works, but something keeps bothering me.

By the way, I'm running my solution from within Powershell 7, if that makes any difference.

Here's my solution:

> adb shell "ip addr show wlan0 | grep -e 'inet[^6]'"
    inet brd scope global wlan0

It works, but I cannot use \d as a character class to filter, which would be preferable.

My preferred solution would show JUST the IP address and nothing else. This is the best I could do:

> adb shell "ip addr show wlan0 | grep -o '^    inet [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9]'"
    inet 192.168.0

Android seems to ship with a limited terminal setting and uses 'toybox' to emulate the typical bash tools. They seem quite limited though, and not very much documentation is provided.

Since I can't use digit character classes or quantifiers, I can only filter for the line, and using -o is the only way to output information. So, my preferred solution is effectively impossible to do.

What can I do to extract just my local IP address with this limited toolset?

  • Just throwing a stone here: why can't you use the \d? Could you try to escape the *\* using \\d?
    – k.Cyborg
    Jun 8 at 22:40
  • I tried that and many variations. Escaping with double ``, using Powershell's escaping method, variations of quotes etc. and I couldn't find one that let me use standard character classes.
    – Xevion
    Jun 8 at 23:00

1 Answer 1


You could just run the grep locally rather than as part of your adb expression:

$ adb shell "ip addr show wlan0" | 
  grep -o -P  '^ *inet \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'

The -P option there means --perl-regexp, and is necessary to support the \d escape.

You could also just use a combination of awk and cut:

$ adb shell "ip addr show wlan0 | awk '\$1 == \"inet\" {print \$2}' | cut -f1 -d/"

There's a bunch of escaping there to prevent the local shell from expanding things like $1 locally.

Or heck, forget traditional tools like grep and awk and do it using JSON and jq:

$ adb shell "ip -j addr show wlan0"  | jq -r '.[]|select(.ifname).addr_info[]|select(.family=="inet").local'

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.