In this series we are exploring various tips, tricks and Bash command line examples which will help you become a more advanced Bash user and coder. Bash provides a rich scripting and coding language which puts the power back in the hands of the user and developer. Bash also allows you to learn as you go along, thereby making it a more enjoyable experience. For the first article in our series, please see our article Useful Bash command line tips and tricks examples part 1.

In this tutorial series you will learn:

  • Useful Bash command line tips, tricks and methods
  • How to interact with the Bash command line in an advanced manner
  • How to sharpen your Bash skills overall and become a more proficient Bash user
Useful Bash command line tips and tricks examples - Part 2
Useful Bash command line tips and tricks examples - Part 2

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # - requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires linux-commands to be executed as a regular non-privileged user

Example 1: Catching that illusive IP address


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


At times, our scripts need to know what IP address our machine is. There are a few different methods, though none of them are fully foolproof. For example, having various network adapters in one computer may create changes in the output by various commands. Also, some commands have limitations.

For example, the hostname command clearly states in it’s manual (for the -I option) that it will display all network addresses of the host, but to not make any assumptions about the order of the output. It thus seems that using hostname may not be the best option.

The examples provided here are to empower you and provide some suggestions for further exploration and environment specific and/or general improvement. Knowledge of the machine being queried, or the network being used will allow one to transform these commands into more stable IP address retriever scripts.

$ MYIP="$(ip a | grep 'inet.*global' | grep -v docker | sed 's|.*inet ||' | grep -o "^[\.0-9]\+")"; echo "${MYIP}"
10.10.0.20
$ MYIP="$(ip -s route get 1 | grep -o "src [\.0-9]\+" | grep -o "[\.0-9]\+")"; echo "${MYIP}"
10.10.0.20

In the first command, we used the ip a (IP address) command to retrieve a list of all IP addresses assigned to this machine. You can type the ip a command directly at the Bash command line to see what the output looks like.

We next grepped all global inet adapters, and removed any docker network connections from the list (you can see this is not perfect; other software’s may create other network interfaces, for example ssh or other virtualization programs like virtualbox, which would also require filtering).

We then process the input further with sed to remove the information up to inet. Finally, we grep for the actual IP with a grep only i.e. grep -o command which uses a regular expression to grab the full (IPv4) IP address.

To learn more about regular expressions, see our Bash regexps for beginners with examples and advanced Bash regex with examples articles.

You can also notice again how there are limitations here; how to incorporate IPv6 IP addresses, how to find out what the main IP address is when there are multiple adapters etc.

In the second command, we look for the first route on the machine with ip -s route get 1 and then process the output in a similar fashion. This is potentially more stable to at least find the main IPv4 address of the machine.

Let us know your best method to obtain the IP, and perhaps the primary IPv4 address using Bash scripting in the comments below!

Example 2: The risks of globbing and the need to quote correctly

$ touch a b c
$ echo "$(echo "*")"
*
$ echo $(echo "*")
a b c


In this example, we first create 3 files, a, b and c, and next we run two echo’s. The first echo properly quotes the * resulting from the echo "*" inside the subshell $(), resulting in a literal * being output by the first echo in the command. The second echo command does not properly quote the * and as such Bash globbing takes place; the * is seen as a filename identifier, and the files are being listed and passed to the fist echo. To clarify this, consider:

$ echo *
a b c

We thus see a need to always properly quote any text which may be interpreted as a filename, even if such a text is already quoted correctly inside the subshell, as is the case here ($(echo "*"): note the quoted *).

Conclusion

In this article, we looked at how to obtain an IP address using various methods and considered expanding this to suit various environments. We also explored Bash globbing, and the need to quote texts which may be interpreted as filenames correctly, in order to avoid globbing.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions