Monday, November 1, 2021

[SOLVED] Get only the subnet mask number on linux using bash

Issue

I have tried this suggestion :

ip -o -f inet addr show | awk '/scope global/ {print $4}'

but that outputs IP address with the subnet mask number:

192.168.1.108/24

I only want the number 24


Solution

this should only output the two or single digit subnet mask number like 24 :

ip -o -f inet addr show | grep -Po "/\K[[:digit:]]{1,2}(?=.*scope\sglobal)"

if you want it to output with the slash /24 :

ip -o -f inet addr show | grep -Po "/[[:digit:]]{1,2}(?=.*scope\sglobal)"



Answered By - tuxutku