The Bubble


Resolve a hostname to IP in a bash script

bash run on Linux


In order to use this script, you need to make sure that nslookup has been installed:

apt-get install dnsutils

The bash script that resolves an IP from a hostname is:

set `nslookup | grep Add | grep -v '#' | cut -f 2 -d ' '`
echo $1

The following script uses a fancy combination of grep and cut to display solely the IP of the hostname, which is resolved using nslookup.

If you use the set `...` line in your script, you will be able to reference the desired IP anytime during your script by calling the variable: $1

** Keep in mind that the ` in the set line are NOT single quotes, but are from pressing the button above TAB.

Reference URL:

Submit a Comment:

We are sorry but we have had to suspend the commenting function due to mass amounts of spam congesting the system. Sorry for the inconvience.