Doing a class with writing a bash scrip for diagnostics.

the bit i'm completely stuck on is how to i only capture a specific line and segment of text in that line.

IE: if i do an ifconfig

 [user@localhost ~]$ ifconfig
 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet  netmask  broadcast
        inet6 fe80::fbaa:d42c:24db:dca7  prefixlen 64  scopeid 0x20
        ether 00:0c:29:bc:c1:4d  txqueuelen 1000  (Ethernet)

I would like bash to capture line 2 column 12 to 27 for ipv4 address into a $string, and continue to do the same for netmask, broadcast, mac address and ipv6 address/cidr...
also do the same to get out any nameservers in the resolve.conf file.

I then of course have to repeat this for each interface...

Unless someone can reccomend a better way of doing this.

I also am wondering if there is a way i can insert a $string to the ifcfg-ens33 file to change the IP address (and everything else) or am i best off completely recreating the file?

i think with this information i should be able to do the rest of the prac quite easily

EDIT: I can see i need to do something with fields, but i can work out how to make it only do field 2 of line 2 into $ipv41

EDIT2: i have come up with this after more reading. but i cant get the output to go into the variables so far. tried echo, printf, >, >>, >>>

#import settings from ip command
#discover interface type into string
/sbin/ip -o -4 addr show up primary scope global| awk '{print $2}' | echo > 

#discover interface ipaddress by interface type into strings
/sbin/ip -o -4 addr show $if1 | awk '{print $4}' | echo > $ipv4
/sbin/ip -o -4 addr show $if1 | awk '{print $6}' | echo > $bast
/sbin/ip -o -4 addr show $if1 | awk '{print $9}' | echo > $dhcp
/sbin/ip -o -6 addr show $if1 | awk '{print $4}' | echo > $ipv6

#print discovered info to screen
echo -e "---- Interface     = $if1"
echo -e "---- DHCP Enabled?   $dhcp"
echo -e "---- IPv4 Address  = $ipv4"
echo -e "---- Broadcast     = $bast"
  • honestly i haven't tried anything yet, as i haven't been able to understand anything i have googled. the most complex line i have used so far in this prac has been. "sudo nmap -sT -O localhost | grep open" Apr 26, 2018 at 3:06
  • 1
    The Bash Guide is a good place to start learning. The rest of that site is also pure gold, even if brusque. I'd advise to stay clear of the Advanced Bash-Scripting Guide. Also, ip is recommended over ifconfig these days, and it can produce much more easily parseable output.
    – l0b0
    Apr 26, 2018 at 3:34
  • Instead of ... | echo > $if1, what you want is if1=$(...), for example if1=$(/sbin/ip -o -4 addr show up primary scope global | awk '{print $2}'). You might want to read on shell command substitution. I hope this helps!
    – filbranden
    Apr 26, 2018 at 6:46

2 Answers 2


Well there's always head, tail and cut, but this might be easier:

echo `ifconfig wlo1` | 
{ read a b c d e inet f netmask g broadcast h inet6 i ; echo $inet $inet6 ; }

Output: fe80::b400:3b2e:a40a:9f19


  • read names and sets the variables in one shot. The a b c d e etc. are dummy variables used to absorb the unwanted ifconfig output.

  • As soon as the } is run, the variables cease to exist. So put any code that uses those variables before the }.


Try this:

ifconfig -a | tr -s ' ' | awk -F ' ' '{if (/^[a-z]/) printf  $1 " "} {if (/inet /) print " " $3" "$5" "$7} {if (/ether/) print " " $2"}'

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.