5

If I were to type a certain command, it would give me a long output in the terminal. I only need a few characters of this output. How do I, in a bash script, save only certain characters from a terminal output as a variable?

For example, if my script is:

#!/bin/bash

ifconfig

I could save the whole output as a variable using

OUTPUT="$(ifconfig)"

But I only want my IP address copied, which is after inet [IP ADDRESS]

How would I go about doing this?

Edit: IP Address is just being used here as an example as I cannnot mention the command on here for security reasons, it's actually 'phrase: [five digit number]' in amongst other text and I just need that five digit number. Sorry for any confusion

9

Using grep

ifconfig | grep -oP '(?<=inet addr:)[\d.]+'

This uses grep's Perl-style regular expressions to select the IP address that follows the string inet.

So, to save that in a variable, just put both commands inside the $():

output=$(ifconfig | grep -oP '(?<=inet addr:)[\d.]+')

The above will save the IP addresses for all active interfaces on your system. If you just want to save the output for one interface, say eth0, then use:

output=$(ifconfig eth0 | grep -oP '(?<=inet addr:)[\d.]+')

Using awk

ifconfig eth0 | awk -F'[ :]+' '/inet /{print $3}'

/inet / selects lines that contain inet. On those lines, the third field is printed where a field separator is an y combination of spaces or colons.

Using sed

ifconfig eth0 | sed -En 's/.*inet addr:([[:digit:].]+).*/\1/p'

Alternate ifconfig

There is another version of ifconfig which produces output like inet 1.2.3.4 instead of inet addr:1.2.3.4. For that version, try:

ifconfig | grep -oP '(?<=inet )[\d.]+'

Or:

ifconfig eth0 | awk '/inet /{print $2}'

Or, use this sed command which works with either version of ifconfig:

ifconfig eth0 | sed -En 's/.*inet (addr:)?([[:digit:].]+).*/\2/p'
  • addr: present in between inet and the ipaddress – Avinash Raj Jul 22 '16 at 14:08
  • @AvinashRaj Thanks! (I had answered this while using debian -- sloppy of me.) I just updated the answer to include methods for the ifconfig which prints inet addr:1.2.3.4 while relegating the methods for the other ifconfig whose output looks like inet 1.2.3.4 to the end of the answer. – John1024 Jul 22 '16 at 20:15
4

ifconfig might disappear in the future, it is deprecated in some linux (maybe some ubuntu versions, but read on).

ip from the iproute2 package (should be installed by default) gives

ip addr list

and can be abbreviated to

ip a

and combined with

ip a | grep -o -P '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}(?=/)'

to filter on all IPv4 look alike numbers with a following CIDR mask slash. If you don't mind about the broadcast, it even works in -E mode of grep, just leave the last brackets off the RegEx.

If you generally want to cut the cli output, and want to avoid piping it through cut ... (like in this example to get the first field)

IPS=$(ip a | grep -o -E '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | cut -d\  -f1 - )

you could use bash string manipulation (like in the following example):

echo ${SSH_CONNECTION%% *}

This should give you a few clues where and how to start.

  • Your answer is OK , but can further be simplified to ip -4 -o addr | awk '{print $4}' to list all addresses. Or $ ip -4 -o addr show wlan7 | awk '{print $4}' to show only wlan7 for example. -o makes output formatted such that it's easy to process via grep or awk and -4 gives only IPv4 addresses. – Sergiy Kolodyazhnyy Jul 22 '16 at 22:29
1

Simple way to print only the IP address regardless of interface using Grep; since that was the question

ifconfig |grep -o -P '(?<=addr:).*(?= Bcast:)'

addr: is the start string

Bcast is the end string

Anything in between those two strings gets printed using .*

1

Using facter:

facter ipaddress

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.